Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Use Multiple React Context Providers in the Same App

    Dave CeddiaDave Ceddia
    reactReact
    ^16.6.0

    When you have two unrelated kinds of data such as a current user and a currently-selected email, it’s common for some parts of the app to need the user and some other parts of the app to need the email. One way to do this is to create multiple Contexts, each with its own Provider and Consumer, and use each one to distribute one kind of data to the slices of the app that need it. In this lesson we’ll pass user data and email data to separate subtrees using two separate Contexts.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Let's make this email app actually display some emails. To do that, we're going to make a new context, in a file called email-context.js. We'll import react and the fetchEmails function from our API. Then we'll create a new context with react.createContext. Then we'll destructure that into provider and consumer.

    Then we'll create a class called email provider to hold the state and distribute it with the provider. We'll initialize the state, total emails. which will be an empty array, current email, which will start off as null, error, which also starts as null, and a loading flag, which starts out false.

    Then we'll implement the componentDidMount life cycle method to fetch the emails. In here, we'll this.setState. The loading true, and reset any error that we might have had. Then we'll call fetchEmails. Then with the emails, I'll call setState, set loading to false, and save the emails into state.

    If there's an error, we'll catch it, and call setState with loading false, and the error. We'll add a method called handleSelectEmail that'll take an email and call setState to update the current email. This will give consumers a function to call when they want to change the current email.

    For render, we'll return the provider, which will render out this.props.children. The value of this provider is going to be an object that's a copy of state, plus another property, called onSelectEmail. We'll pass in our handleSelectEmail function. Finally, we can export this email provider class and consumer as emailConsumer.

    Now that we have a provider, we can go back to index.js and at the top, we'll import emailProvider from our new email context file. At the bottom, we can wrap route in email provider. Now, the whole app will have access to emails and the current user. Now, let's open up message list, and we'll import our email consumer from emailContext.

    Since this component is already consuming one context, we can just add our emailContext inside. We could also put this outside of userConsumer. It doesn't really matter here. Inside the emailConsumer, we'll write the function that's going to pull out loading, emails, and onSelectEmail from context.

    Now we have all this data. We just need to render it out. If we're loading, we'll render a loading message, otherwise if email's length is zero, we'll render out the mailbox is empty message that we already have. Otherwise, if we have messages, it'll render out an unordered list. Inside that list, we can map over the emails, with emails.map.

    For each email, render out an email component, which we'll create in a second, passing the required key prop, with the email's unique ID. Down below, we'll create this email component. It's going to need the email to display, and an onClick prop. It'll render out a list item, passing along the onClick prop to make it clickable.

    Inside, we'll have a div for the subject, that displays email subject, and a div for the message preview. Now, we can pass these props along to the email, passing in the email itself, and an onClick prop, which will be a function that calls onSelectEmail with the email. Now, it's all wired up, and we can see the emails displayed, but clicking on them doesn't do anything yet.

    Let's open up the main page file, and we can see right now this component is only ever rendering a list. We need it to render either a list or the currently selected email. To do that, we'll pull in emailConsumer from emailContext, and wrap the content with this consumer. Inside here, we'll pull out the current email property from context.

    If we have a current email, then we'll render a message viewer. Otherwise, we'll render the message list. We just need to import messageViewer and now, when we click on an email, we're taken to the message display. Let's work on this display now. We'll open up messageViewer and import the emailConsumer. 

    Then we can wrap this content in an emailConsumer, and pull out the current email and onSelectEmail from context. Inside here, we'll replace this with an h2 to display current email subject, and a div, to display current email body. 

    At the top, we'll add a button for navigating back, and give an onClick prop that'll call onSelectEmail with null to reset the selected email, and bring us back to the list. Now, we can click an email, read the message, click back, and it's all working.