What are hooks, and what are the rules of hooks?
Model answer
Hooks are functions such as useState and useEffect that let function components use state and other React features. There are two rules: call hooks only at the top level of a component or custom hook (not inside loops, conditions or nested functions), and call them only from React function components or custom hooks.
React relies on the call order being the same on every render. The eslint-plugin-react-hooks package checks these rules for you.
Learn this topic
React useEffect Explained: Side Effects, Dependencies and Cleanup in the course React.js Full Course: Beginner to Advanced.
Sign in to add your own answer.
More questions
- What does useEffect do?
- What does the dependency array of useEffect mean?
- Why does an effect run twice in development?
- What is the cleanup function in useEffect, and when does it run?
- How do you fetch data in a React component with useEffect?
- What are race conditions in effects and how do you prevent them?