Using Hooks
Hooks can only be used in:
- React function components
- Custom Hooks (we are going to learn about creating custom Hooks in the next chapter)
Hooks cannot be used:
- Inside conditions or loops
- After a conditional
return
statement - In event handlers
- In class components
- Inside functions passed to Memo, Reducer or Effect Hooks
- Inside
try
/catch
/finally
blocks
In some places, like the React docs, using a Hook is sometimes called calling the Hook.
Hooks are normal JavaScript functions, except that React relies on them being invoked from inside a function component. Of course, custom Hooks that use other Hooks can be defined outside of React function components, but when using those custom Hooks, we always need to make sure that we call them inside a React component.
Next, we are going to learn about the rules regarding the order of Hooks.