InterviewHubPRO
Section Overview
Q272

Question 272 of 445

Part 9: React Hooks Deep Dive (262 - 316)

61%
React & Modern Frontend (445 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

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,

js
user.name = "Alex"; //wrong way
setUser(prev => ({ ...prev, name: 'Alex' })); //correct way