Sub topic 4
Hooks share logic, not state
This chapter assumes you know useState, function components, and how to call a custom hook such as useToggle. It explains why two components, or two calls in one component, never share state through a hook.
Logic is shared, state is not
A custom hook is a function. When a component calls it, React runs the hook's body during that component's render. Each useState inside the hook creates a state slot that belongs to the calling component, at the position of that call.
Calling a hook twice gives two separate sets of slots. Two components that use the same hook also get separate slots. The hook is one piece of code, and every call to it gets its own state.
Here is the hook from the previous chapter:
import { useState, useCallback } from 'react';
export function useToggle(initial = false) {
const [on, setOn] = useState(initial);
const toggle = useCallback(() => setOn((value) => !value), []);
return [on, toggle];
}Two calls, two states
This component calls useToggle twice:
import { useToggle } from './useToggle';
function Panels() {
const [openA, toggleA] = useToggle();
const [openB, toggleB] = useToggle();
return (
<>
<button onClick={toggleA}>{openA ? 'Hide A' : 'Show A'}</button>
<button onClick={toggleB}>{openB ? 'Hide B' : 'Show B'}</button>
</>
);
}Clicking the first button changes only openA. The second button keeps its label. Each call to useToggle registers its own useState, so React stores two independent values for Panels.
How React keeps the calls apart
React does not identify state by name. It identifies state by the order of hook calls in a component. On the first render it records the slots in order. On later renders it matches each call to the same slot by position.
This is why the Rules of Hooks forbid calling hooks inside conditions or loops. If the order changed between renders, a call would receive another call's state. Custom hooks follow the same rule, because their inner hooks join the calling component's list.
Sharing state is a different job
Sometimes you do want components to see the same value. A hook cannot do that on its own. Choose one of these approaches:
| Goal | Approach |
|---|---|
| Two siblings use one value | Lift the state to their parent and pass it down as props |
| Many distant components use one value | Put the state in context and read it with `useContext` |
| Value lives outside React | Subscribe with `useSyncExternalStore` (covered in a later chapter) |
A hook can wrap any of these. For example, useTheme can call useContext inside. The value is shared because of the context. The hook only makes access convenient.
A common mistake
Developers sometimes expect const [open, toggle] = useToggle() in two different components to stay in sync. It will not. Each component's open starts from initial and changes only when its own toggle runs.
Likewise, a module-level variable inside the hook file would be shared by every caller. That would be shared mutable data outside React's control, so React would not re-render components when it changes. Avoid it unless you pair it with useSyncExternalStore.
Exercise
A Sidebar component and a Header component both call useToggle(). A user clicks the toggle in Sidebar. Does the value in Header change? Why, and what would you do if you wanted both to change together?
Show answer
No. Each call to useToggle creates its own useState slot inside the component that calls it, so Header keeps its own value. To share it, lift the state into a common parent and pass the value and toggle function down as props, or put them in context and read them with useContext.