The initial state value. Can be any type: number, string, boolean, object, or array.
Returns
A two-element array:[0]— The current state value.[1]— A setter function that updates the state and triggers a re-render.
Usage
Counter
Toggle visibility
Object state
Notes
- Each component instance maintains its own independent state. Two instances of the same component do not share
useStatevalues. - The
initialValueis only used on the first render. Subsequent re-renders return the current state. - Calling the setter with the same value may still trigger a re-render.
- For state shared across multiple components, use useStore or useAppState instead.
useStatemust be called inside a componentbodyfunction, not at the top level.
See Also
- useStore — global shared state
- useAppState — simple global key-value state
- withAnimation — animate state changes