+1 vote
154 views
by (98.9k points)
C When are the React components re-rendered? Explain giving examples. 05

1 Answer

0 votes
by (98.9k points)
 
Best answer
import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}

export default Counter;

 

In this example, the Counter component has a state variable count. When you click the "Increment" button, it triggers a state change using setCount, leading to a re-render of the Counter component to reflect the updated count value in the UI.

 

 

 

 

Related questions

+1 vote
0 answers 150 views
+1 vote
1 answer 52 views
0 votes
0 answers 19 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

504 users

...