4 ms·
I find this to be a very nice high-level explanation: https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e https://medium.com/@ryardley/r
by danabramov 4y ago
I find this to be a very nice high-level explanation: https://medium.com/@ryardley/react-hooks-not-magic-just-arrays-cd4f1857236e https://medium.com/@ryardley/react-hooks-not-magic-just-arra...
In reality we use a linked list rather than an array. If you wanna dive into the code, I can give some pointers. For example, useState is implemented like this during first render (https://github.com/facebook/react/blob/87c803d1dad7e5fe8863436b0d2d18df3d462f21/packages/react-reconciler/src/ReactFiberHooks.js#L1826-L1846 https://github.com/facebook/react/blob/87c803d1dad7e5fe88634...) and like this during next renders (https://github.com/facebook/react/blob/87c803d1dad7e5fe8863436b0d2d18df3d462f21/packages/react-reconciler/src/ReactFiberHooks.js#L1848-L1852 https://github.com/facebook/react/blob/87c803d1dad7e5fe88634...).
However, _conceptually_ I'd recommend to think of Hook return values similar to "extra inputs" to your function, kind of like extra arguments. There are different ways to formalize it in different languages. We picked plain function calls for simplicity and low overhead, although you could imagine `yield` with generators or something like that.
- imbnwa 4y ago>In reality we use a linked list rather than an array Why a LinkedList rather than an array?