Question · JavaScript and React course · Module 4: Context API and useReducer

Custom Hook for Context API

Why create a custom hook (e.g., useAuth) to consume context instead of using useContext directly?

Answer options

  • A custom hook is required by React, direct useContext doesn't work
  • A custom hook automatically optimizes context performance
  • Encapsulation of logic, validation that the Provider exists, and a cleaner API for consumers
  • A custom hook allows using context in class components

You can check the correct answer by solving this task in the CodeWorlds game.