11 ms·
1. They work by setting a global variable to the value of the current component then calling the render function. Whenever you call a hook you're effectivelly d
by spion 4y ago
1. They work by setting a global variable to the value of the current component then calling the render function. Whenever you call a hook you're effectivelly dispatching it to the component in question, OOP style.
2. React counts the number of executions of (certain) hooks. This count is how it knows which state to get from the store as a return value from `useState`. useState is effectively `getStateAndSetter()` but it doesn't pass a key name of any kind, so the implicitly passed key is `hookCount++`. This is why you can't call hooks conditionally, or state would get all messed up - if a condition turns false and one hook doesn't run that render, all getStateAndSetter calls that run after it will be off by one.
- paganel 4y agoI know almost nothing about React, let alone React hooks, but all this looks a little hack-etty, doesn't it? Also, I'm curious if there is any similarity between these React hooks and the infamous Drupal hooks.
- spion 4y agoIts absolutely hacketty, yes. Not familiar with Drupal.
- rizky05 4y agoIt’s different but has similarity. Both are function that gets called when some lifecycle is reached. React hooks is running on each component, not on whole app. React app is composed by bunch of components, and each of those have their own hooks.