Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>because under the hood the React runtime is storing all the hooks you call in an array and firing them off by iterating through the array.

I must correct this. Pay attention to this function [0]

It's just a plain linked list and nothing is being fired off by iterating through the linked list. When you call a hook react either creates a new node in the linked list or it pops the current head off the linked list so that the second hook will get the second element, the third hook the third element and so on.

The hooks are fired off by calling them. React isn't calling them again. Each render simply calls your component function and your function calls the hooks which runs a reducer [1] on the internal queue of the hook to compute the latest state. The second return value is just pushing elements into the internal hook queue.

[0] https://github.com/facebook/react/blob/e07039bb61e3d006ad552...

[1] https://github.com/facebook/react/blob/e07039bb61e3d006ad552...



Yes, you're right. Iterating is not the right word, and it's the ordering of the data in the internal queue that I was thinking of, which is fixed by the order in which the hooks are called. During rendering, your component itself will run through the queue. It's not React doing it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: