React Docs says:
How does React associate Hook calls with components?
React keeps track of the currently rendering component. Thanks to the Rules of Hooks, we know that Hooks are only called from React components (or custom Hooks — which are also only called from React components). There is an internal list of “memory cells” associated with each component. They’re just JavaScript objects where we can put some data. When you call a Hook like useState(), it reads the current cell (or initializes it during the first render), and then moves the pointer to the next one. This is how multiple useState() calls each get independent local state.
First of all... what is this "memory cell"? Can we print it out ourselves to see its value?
Second, there is only one instance of the function (such as Counter()
in the following example). So if we render <Counter />
twice into the DOM, once to <div id="root1">
and the second time to <div id="root2">
, how does the function Counter()
know which is which?
function Counter() {
let [count, setCount] = React.useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}> + </button>
{ count }
<button onClick={() => setCount(count - 1)}> - </button>
</div>
)
}
useState is a Hook that allows you to have state variables in functional components. You pass the initial state to this function and it returns a variable with the current state value (not necessarily the initial state) and another function to update this value.
So how does React know which state corresponds to which useState call? The answer is that React relies on the order in which Hooks are called. Our example works because the order of the Hook calls is the same on every render: // ------------ // First render // ------------ useState('Mary') // 1.
Output: The value returned by useState() consists of an array with two values. The first value is the initial (or starting) value of the state variable, while the second value is a reference to the function that can be used to update the variable.
Every level of your React application has a key. When using JSX, you can specify the key explicitly using: <Button key={id} /> . If no key attribute is provided, React will use the index of the component (in the current level) to implicitly add a key property to the component.
Let's say you have two of your counters inside a parent container JSX:
<div>
<Counter/>
<Counter/>
</div>
This is how React can 'know which is which' (extremely simplified):
const componentStates = [ { value: 13 }, { value: 27 } ]; //"memory cells"
let context;
// the parent container JSX will be transformed/executed in this way:
function render() {
context = componentStates[0];
Counter();
context = componentStates[1];
Counter();
}
function useState() {
return [ context.value, updateFunction ];
}
function updateFunction(newValue) {
context.value = newValue;
render();
}
A key concept to understand this is, that React.useState
is not a pure function, it relies on the rendering context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With