Become a member
to unlock all features

Level Up!

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


    Refactor a Class Component with React hooks to a Function


    We have a render prop based class component that allows us to make a GraphQL request with a given query string and variables and uses a GitHub graphql client that is in React context to make the request. Let's refactor this to a function component that uses the hooks useReducer, useContext, and useEffect.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: Let's start out by making a function called query, and we're going to take all of the same props that we took before. We'll say query, variables, children, and normalize. Awesome. Normalize has a default value, so we're just going to use the destructuring default value right there.

    We've taken care of our props. Let's go ahead and pull the prop types onto our query now as a static property on that function. Then we can get rid of the prop types and the default props. Next, let's take care of the context type here.

    We're going to pull in useContext from here, and we'll get our client from useContext, github.context. Perfect. We got rid of that. Now, we need to take care of the state. There are a couple ways we could go about doing this.

    One way we could do this is, each one of these gets its own useState hook call, but instead, I'm going to change this as minimally as possible. What I'm going to do is, actually, we're going to pull in use reducer.

    Then in here, I'm going to get my state. I'm going to call this dispatch actually setState. I'm going to make this setState function behave similar to how setState works in a class component. Here, if we look, we're calling setState with an object here and an object here.

    We're not using the function version of this setState API. Let's go ahead and implement a reducer that implements that same kind of API. We'll say useReducer, and here, we're going to get our state and the new state.

    Here, we'll just merge in the state with a new state. Then we'll specify our initial state right here. Let's just pull that in from there. We've taken care of our state. Now, when this query component is mounted, it's going to call this query function, which basically just does all of this stuff.

    Let's copy this into a useEffect hook. I'm going to pull in useEffect. Then we'll come down here, useEffect. In our callback here, we'll just paste all of that stuff. Anywhere we see this, we can get rid of that.

    Because we implemented our useReducer reducer to behave similarly to setState, we can actually just get rid of this off of setState. Here, we're using this save setState, which we'll deal with later. We'll just call setState right now.

    Then we've also got this context. We're not going to need to get it from this anymore. We have access to it directly in our function. I'll just get rid of that line, and now, this client is referencing useContext there.

    Cool, all right. Then instead of this.props, we're getting our props directly from the function signature. I'm just going to pull this.props all three of those places, and get rid of that there. Sweet. We're off to the races there.

    Now, the next thing that I need to do is, when our component updates, we want to make that query again. We're actually doing that. Every single time our component updates, this callback function's going to be called.

    We actually only want to call this callback function when our query and our variables have changed. We're doing that in our old class component here with componentDidUpdate. We verify that the query and variables have changed.

    If either one of those have, then we'll re-run the query. We want to simulate that same behavior with our useEffect hook. As a second argument to here, I'm going to pass in my query and variables. Perfect.

    We've taken care of componentDidMount, with the exception of this._isMounted. We'll deal with that later, and our component did update. Perfect. Then we got our component will unmount. We'll take care of that later.

    Then we took care of the query as well, and our save setState, which we'll take care of later as well. We're just left with a render, and that's basically what we return from our function here, is just return. We're no longer using this props anymore.

    We just return children with the state. We're now getting that state directly from this user reducer. It's just a variable in the closure of our query component. We have our context coming from useContext. We're providing GitHub.context, which we're using the context type before.

    Then we're also getting our state from userReducer. We initialize that state, just as we were before. With our new state and our state, we merge those together, similar to how this.setState works in React.

    Then we have this useEffect, so that this callback will run when the component is mounted, and whenever the component is updated, in the case that query and variables has changed. We're going to rerun this update callback every time one of those changes.

    Then we return our children prop. We're going to invoke that to provide this nice renderProp API. One last thing to do here is we'll get rid of that class component. We'll get rid of this component here. We'll also get rid of low dash is equal.

    We'll save the file, and now, if I try to go to Kent C. Dodds, we're loading that data, and everything is working just as it was before. We can go to Matt Sabriski, and we load Matt's data. Our API for our query component hasn't changed one little bit.

    The implementation itself has changed, and been made quite simpler, by using React hooks like useContext, useReducer, and useEffect.