How do you decide where state should live?

React state and events · Experienced

Model answer

Start with the nearest component that uses it. If siblings need it, lift it to their parent. If many distant components need it, use Context or a store. Separate kinds of state: UI state (modals, inputs), server data (use a data library such as TanStack Query) and URL state (filters and pages belong in the URL so links work).

Avoid duplicating state; derive values during render instead of storing them in another state variable.

Learn this topic

React useState Explained: Make Your Page React to Change in the course React.js Full Course: Beginner to Advanced.

Sign in to add your own answer.

More questions