3 ms·
The simplest version is: - React has a module-scoped variable in the hooks implementation file that tracks which component is currently rendering - The intern
by acemarke 4y ago
The simplest version is:
- React has a module-scoped variable in the hooks implementation file that tracks which component is currently rendering
- The internal "Fiber" data structure that describes a component instance has a linked list of hook contents (saved values and callbacks)
- As each hook gets called, React tracks the current hook entry, looks up the current hook data, and returns it
So, the number of hooks used needs to be the same each time the component renders so that the linked list entries match up consistently.
There's a great talk by @swyx here that builds a miniature hooks implementation in about 30 minutes:
https://www.swyx.io/hooks https://www.swyx.io/hooks