useState function provides component-local state management similar to React’s useState hook. It returns a tuple containing the current state value and a setter function to update the state.
Parameters
initialValue- The initial value for the state. Can be any type.
Returns
Returns a tuple[currentValue, setterFunction] where:
currentValue- The current state valuesetterFunction- A function to update the state that triggers component re-renders
Usage
Basic state management
With complex state
Notes
- State updates trigger component re-renders automatically
- Each component maintains its own state using hook indexing
- The setter function can accept the new value directly
- Initial value is only used on the first render
