What's the difference between a JavaScript function and a React hook?
Answer a question
I've been looking into writing my own React hooks but so far am struggling to tell on a technical level what the difference is between a normal JavaScript function and a hook.
For example, this article says hooks are "normal JavaScript functions which can use other hooks inside of it", but what else is going on under the hood?
I know that we use the word use in the hook name, but I'm curious what it is about hooks that make them hooks and not functions!
Answers
what it is about hooks that make them hooks and not functions!
Hooks are functions. What's special about them is what their purpose is and when they're supposed to be used. Their purpose is to give you a way to interact with the react component lifecycle, and they are implemented so that they work correctly only if they are called while a functional component is rendering.
The react team has implemented 10 of these functions. They let you do things like manage state or run side effects. You can mix and match these any way you like, and if you put the code into a helper method, this is referred to as a "custom hook".
更多推荐
所有评论(0)