React question detail
What types of values can `useState` hold?
The useState hook accepts different types of values.
- Primitives:
number,string,boolean - Arrays
- Objects
- Functions
nullorundefined
But you needs to be cautious with reference types (objects/arrays) because React compares old and new values by reference, so direct mutations won't trigger a re-render. For example, the correct and wrong ways of state updates as shown below,
user.name = "Sudheer"; //wrong way
setUser(prev => ({ ...prev, name: 'Sudheer' })); //correct way