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
Become a member
to unlock all features

Level Up!

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


    Add Redux with Server-to-Client State Hydration to a React Universal Project

    Tim KindbergTim Kindberg

    We will add redux to our project and get redux working on the client and server. The redux state will be passed along with our server-side rendered page in a global window variable; from there, we’ll hydrate the state on the client.



    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




    Instructor: I've added Redux to my project. I'm no longer controlling the selected state internally to the component. I'm actually using two props that I'm getting from Redux, selected, and select tab, to control the selected state and set the selected state. Select tab is an action that I have. Get selected tab is a selector that pulls the selected tab off the state.

    At the bottom, I am mapping in the selected tab to the prop called selected. I'm mapping in select tab to a prop called select tab. I'd like to be able to click on each tab and change the state in Redux. Let's see how we can make Redux work with React universal component.

    Let's set up our provider in the index. I need to import the provider. I'm going to import a function called configure store from a file that I've not written yet. The configure store file will just be an isolated place that we can set up the Redux store. It's going to return a store. It's going to accept an initial state.

    When you render on the server, you're already rendering your app, but really your app is nothing without your state. You want to make sure you're rendering your app with the proper Redux state already baked in. That will just work.

    However, if you don't pre-populate your store on the client, then it has to do that work all over again when it loads on the client. It's best to just sort of tell Redux, "Here's the state. I've already figured it out."

    The simplest place to store that is just on a private property on window. We're just going to add the state to this private window property called initial state. Now let's use our provider. We'll set the store on the provider to the store that we created.

    We need to create a new file called configure store. We want to import create store from Redux. We want to import our reducer from reducer/index. We're going to go ahead and create our method here called configure store.

    Remember, we're going to be taking in the initial state. That's coming off the window. That's the window initial state. Here we want to just use the actual create store from Redux, pass it our reducer and our initial state. We're going to return the store. I just want to export this as the default.

    Another neat thing you can do here is actually set up hot module reloading for your reducers. If exists, then we want to module hot accept any changes to the reducers.

    If those change, we're going to go ahead and just use the store.replace reducer. We're going to import a fresh copy of the reducers and put those into the store and replace the old ones.

    Now, we need to pre-populate the state on the window. We're going to have to go to the server's render JS file to do that. We need to import the provider from React Redux just like we do one the front end. We're also going to need that configure store function that we just wrote.

    Let's go ahead and create the store. This time, we're not going to give it an initial state because this is the first time we're actually rendering it on the server. Let's figure out what our initial state is by stringifying the store's current state. We need to wrap our app in the provider and then give the provider access to the store.

    Now we go down to our HTML response. We're going to add a script tag. Here's where we're going to add that initial state.

    Let's see if everything worked. We have a syntax error. That's going to be easy to fix, just have to move my comma. Refresh. Bar is loaded. Let's check out what was sent down from the server. If we look at local host, open in the sources panel, we see window initial state is set to our Redux state.

    That's going to be populated straight into the Redux state on the client side. Then we're seeing the state of our app was also reading from our initial state where bar is loaded. That was all coming from our tabs reducer where a selected tab started as bar.

    If I were to change this to home and refresh, we're seeing home is loaded, the initial state is coming down as home, and the initial server side renderer of our app is also home is loaded.