Use a React Context Consumer Outside of the Matching Provider

Share this video with your friends

Send Tweet

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.

Steve
Steve
~ 6 years ago

The code here, like in the first lesson, is the completed version rather than the start code.

Dave Ceddia
Dave Ceddia(instructor)
~ 6 years ago

The 'before' code for this lesson can be found here.