Skip to content
doubtly

2 answers

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.

Doubtly · 19 Nov 2023 Report
Was this answer helpful?

A React component re-renders when its state changes, when it receives new props, when its parent re-renders, when a context value it uses changes, or when forceUpdate() is called.

1. State change: calling the state setter (setCount or this.setState) with a new value re-renders that component.

function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>
           Clicked {count} times</button>;
}

2. Props change / parent re-render: when the parent re-renders, its children re-render too, receiving the new props.

function Child({ value }) {
  return <p>Value: {value}</p>;
}
function Parent() {
  const [n, setN] = useState(0);
  return (
    <div>
      <button onClick={() => setN(n + 1)}>+</button>
      <Child value={n} />   {/* re-renders on every click */}
    </div>
  );
}

3. Context change: every component that reads a context with useContext re-renders when the Provider's value changes (for example, switching a theme from light to dark).

4. forceUpdate(): in class components, this.forceUpdate() re-renders without a state or prop change.

No re-render happens if you change a variable directly (e.g. count = count + 1) or mutate state without the setter, and setting state to the same value is skipped. Unnecessary child re-renders can be avoided with React.memo, PureComponent or shouldComponentUpdate.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.