4 ms·
I can describe both of those pretty simply (I think). React uses simple objects called "fibers" to track metadata for every component in the tree. When you be
by acemarke 7y ago
I can describe both of those pretty simply (I think).
React uses simple objects called "fibers" to track metadata for every component in the tree.
When you begin rendering a function component, React creates a linked list of hooks, and attaches the linked list to the fiber so it knows what hooks exist for that component.
Each hook can save some kind of value in the linked list. For `useState`, it's the state value. For `useCallback` or `useMemo`, it's the cached value, and so on.
When you render again, React loops over the hooks linked list, reads the value at that index, and returns it to your component.
Shawn Wang has a talk where he builds a tiny version of hooks in a few lines:
https://www.swyx.io/speaking/react-hooks/ https://www.swyx.io/speaking/react-hooks/