3 ms·
The ordering question is literally just about how you call them within a single specific component. That component can then be used as many times as you want,
by acemarke 7y ago
The ordering question is literally just about how you call them within a single specific component. That component can then be used as many times as you want, in as many apps as you want.
In other words, this is not legal:
const [stateA, setStateA] = useState(false);
if(stateA) {
const [stateB, setStateB] = useState(42);
}
Call as many hooks as you want, in whatever sequence you want. Just make sure all the calls are at the top level of that function component, and that you don't somehow change the sequence of those from one render pass to the next.
As for how they work, React already has metadata that describes each component in the tree. There's a specific field in those metadata objects that gets used for tracking internal component state, and for function components, that field stores an array / linked list of the hook calls you've made and their last saved results.
See these resources for more explanations on how hooks are implemented:
https://github.com/markerikson/react-redux-links/blob/master/react-hooks.md#understanding-hooks-concepts https://github.com/markerikson/react-redux-links/blob/master...