1×
Become a member
to unlock all features
Autoplay

    Use a React Context Consumer Outside of the Matching Provider

    reactReact
    ^16.6.0

    You may come upon a case where you have a generic component that can consume a Context, but doesn’t require that Context in order to work. On the flip side, you may want to validate that a component is always used inside a required parent Context. In this lesson we’ll see how to set a default value on a Provider and access that default in the Consumer, as well as how to validate that a Consumer is used correctly, by checking the value it receives and throwing an error if it is missing.