Question - What are the rules you should follow for the hooks in React?
Answer -
We have to follow the following two rules to use hooks in React:
- You should call hooks only at the top level of your React functions and not inside the loops, conditions, or nested functions. This is used to ensure that hooks are called in the same order each time a component renders, and it also preserves the state of hooks between multiple useState and useEffect calls.
- You should call hooks from React functions only. Don't call hooks from regular JavaScript functions.