What is state in React?

Beginner

Answer

State is a built-in React object used to contain data that may change over the lifetime of a component. When state changes, the component re-renders.

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
    </div>
  );
}

State characteristics:

  • Mutable (can be changed)
  • Local to the component
  • Triggers re-renders when updated
  • Should not be modified directly