Question · JavaScript and React course · Module 15: Patterns and Architecture

Provider Pattern - Validation

Why should a custom hook using context (e.g., useTheme) check whether it is used inside a provider?

Answer options

  • To throw a readable error instead of undefined behavior when the provider is missing
  • To automatically create the provider if it's missing
  • To optimize rendering of components in the tree
  • To prevent the same provider from rendering multiple times

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