ReactAdvanced#state#patterns

How do you share state between distant components?

Options in order of weight: lift state to a common parent, use context for slow-changing global data, use a query cache for server state, and use a store like Zustand or Redux for large shared client state.

Example
// server state
const { data } = useQuery({ queryKey: ['user'], queryFn: getUser });
// client state
const theme = useContext(ThemeCtx);

Related Questions

1
ReactAdvanced#state#data

What is the difference between server state and client state?

Open
2
ReactIntermediate#state

Why should you not mutate state directly?

Open
3
ReactIntermediate#routing

How do you handle routing in React?

Open