E
E
Exploring2021-09-21 22:34:17
JavaScript
Exploring, 2021-09-21 22:34:17

How do react hooks determine which component they belong to?

I’m studying React and the question haunts me: how do useEffect or custom hooks understand which component they belong to and which component needs to be redrawn when the state changes? If the hook is not connected in any way to the component function, but is simply called inside it (if connected, how does this happen?). If possible, answer in detail, well, or with pseudo-code examples.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2021-09-21
@Exploring

Firstly, it stores a Map where the keys are the components and the values ​​are their states
Secondly, it stores a shared link to the current state: https://github.com/facebook/react/blob/main/packag...
Well, probably there is a stack of states (I didn’t dig deep into their sources, but I would do it that way)
Before calling the component function, we send the current state to the stack, in its place we get the state for the required component from the Map , exit, we restore the state for the parent component from the stack.
When running the hook, we simply get the current state and work with it: https://github.com/facebook/react/blob/main/packag...

L
low molecular macro, 2021-09-21
@molekulyarniy

do you know how to speak english?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question