FrontendDeveloper.in

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
  • null or undefined

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
Back to all React questions
Get LinkedIn Premium at Rs 399