4 Typical Scenarios for Custom React Hooks
Why do we need to build our own hooks?
- Custom Hooks can help us write more reusable code.
- Custom Hooks can help us split complex logic and simplify components.
What does this article for?
The official documentation is too brief in the chapter introducing custom hooks, and many important scenarios are not mentioned. So I would want to supplement the official documentation and summarize 4 typical scenarios for using custom Hooks.
If you don’t know anything about React Hooks, please read the official documentation first to know why we should use hooks, and understand some build-in hooks such as
Introducing Hooks - React
Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. This…
How to custom hooks?
Also, if a function has the following characteristics, we will take it as a hook:
- Its name starts with
- Use other hooks inside its function body
For example, this is a hook:
If a function’s name doesn’t start with
use but use other hooks inside its body, then an error will be thrown during the compile phase.