There's a faster version of egghead. Click here to try it.
1×
Become a member
to unlock all features
Autoplay

    Validate Compound Component Context Consumers

    reactReact
    ^16.3.2

    If someone uses one of our compound components outside the React.createContext <ToggleContext.Provider />, they will experience a confusing error. We could provide a default value for our context, but in our situation that doesn't make sense. Instead let's build a simple function component which does validation of our contextValue that comes from the <ToggleContext.Consumer />. That way we can provide a more helpful error message.