What is useState in React?

useState is a React Hook that allows you to add state management to functional components. It returns a state variable and a function to update it.

const [state, setState] = useState(initialValue);

// initialValue can be a value or a function like this
const [state, setState] = useState(() => computeInitialState());

Whenever a state changed => React triggers re-render

But if you call setState with the same value as current value => React skips the re-render for performance optimization since the state hasn’t changed.