4 ms·
Because they "hook" into the component lifecycle. Which is why I'm not sure I understand your complaint... hooks are meaningless outside of a component because
by ng12 5y ago
Because they "hook" into the component lifecycle. Which is why I'm not sure I understand your complaint... hooks are meaningless outside of a component because there's no lifecycle to hook in to. If you have plain-old-javascript with a hook in it something is not making sense.
- k_sze 5y ago> Because they "hook" into the component lifecycle. For me, "hooks" in other frameworks/libraries that I have encountered don't work like that. Usually, the hook is something static and predefined by the framework/library, and _my_ code attaches to those hooks. The way you explain React hooks sounds like we're _conjuring_ hooks in the components willy-nilly, and then attaching things to them. However, I have never found an explanation for the name "hook" in the official React documentation; the explanation might be somewhere in the documentation, but I never found it.
- TheCoelacanth 5y agoThe majority of third-party hooks that I see use useState, useEffect or useContext, which hook into the React lifecycle of the component they are used in. If they aren't using one of those (or more rarely one of the other built-in hooks), then they probably don't really need to be hooks and should just be plain functions.