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

Context API Optimization

How do you prevent unnecessary re-renders in components using Context API?

Answer options

  • Split a large context into smaller ones and wrap the Provider's value in useMemo
  • Always use useRef instead of useState in context
  • Avoid nesting more than two Providers
  • Pass context as props instead of using a Provider

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