Search⌘ K
AI Features

React Hook: useState

Explore how to use the React useState hook to add and manage local state in functional components. Learn to initialize state with simple values or objects, handle input changes, and trigger re-renders reflecting updated state in the UI.

We'll cover the following...

React hooks

Prior to React 16.8, the only way to access state in a React component was by composing with JavaScript’s class component. Functional components weren’t optimal since they were only used as presentational components. React 16.8 changed all that and introduced the concept of hooks, in which a functional component can hook into the React state and the lifecycle methods.

Note: Hooks only work in functional components. They don’t work in class-based components.

The useState hook

The useState hook adds a local state to a React component. See the ...