Skip to content

Latest commit

 

History

History
27 lines (20 loc) · 1.53 KB

File metadata and controls

27 lines (20 loc) · 1.53 KB

Context API - Behaviors

Review, Research, and Discussion

When you have multiple contexts, what component type should you use (class/function) and why?

  • use a function component since you can just import it using the useContext hook and wrap your code in multiple context.provider elements, whereas class components can only consume one context.

What are some good use cases for using the Context API for global state?

  • current authenticated user, theme (typically light or dark), or perferred language

How can you best test context?

  • The best way to test Context is to make our tests unaware of its existence and avoiding mocks. We want to test our components in the same way that developers would use them (behavioral testing) and mimic the way they would run in our applications (integration testing). source

Vocab

context

  • Context provides a way to pass data through the component tree without having to pass props down manually at every level. source

useContext()

  • the hook for utilizing context in functional components

static context

  • A property of the class component

Resources