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

    Refactor a React Class Component with useContext and useState Hooks

    Kent C. DoddsKent C. Dodds
    reactReact
    ^16.7.0-alpha

    We've got a pretty simple User class component that manages a bit of state and uses some context. Let's refactor this over to a function component that uses the useContext and useState hooks.

    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: The component that's using this query component is actually a class component in itself. It's keeping track of a filter state and it needs to get the context and it needs to get the Github context just like our query component did.

    Let's go ahead and change this to use hooks. I'm just going to make a function called user and that's going to take our props. We're only taking username and that does not have a default, but we do have some prop types here so I'll take those out. We'll say user dot prop types equals that.

    Then let's take care of this Github context, so we're going to const client equals use context. Github context. Let's pull in use context from React. I also noticed we're going to want use state, so I'll just pull that in here while I'm up here.

    Next, let's go ahead and use that state. We're taking care of this context. We'll take care of this state now. We just have one item of state, so I'm going to say filter and set filter equals use state. We'll initialize it to the same value that we were initializing it to before. We've taken care of state initialization.

    Then we want to handle filter update. That actually is a function that accepts the filter and sets the filter value. That looks suspiciously like the set filter. We actually don't need to have a handle filter update. If we did then we could just move this right up and make this a const and then use it as we had before. Change this to set filter with that filter.

    That would work just as well, but that's basically just a pass through function, so it's worthless. Let's just get rid of it. Next, we have just our render. That's all that's left. What I'm going to do is I'm going to take this return all the way down to this closing parentheses. We'll put it right up here.

    Then I can get rid of the user class entirely. I'll hit save here. Now the last thing that we need to do to make sure that everything is working is look for this dot. If you find this dot anywhere in a component that should be a function component, then you've got a problem there. You missed something in your refactor.

    That's what we did. We missed dot context dot logout. What I'm actually going to do is I'll just get rid of this dot context. This logout is going to come from client here, so I'll just destructure that and logout. Let's look for this dot.

    There's another one. This is that function that we saw that was totally useless, so we're going to say set filter and this on update is just going to call our set filter with the new filter. Now let's look for this dot again. Looks like we are totally good to go. We can get rid of this component import now and let's make sure that everything is working.

    I'm just going to go. I can't see dots here. Go, loading our data. There it is. It's all working just like it was before, except it is using arguably a simpler function component. We have our context, we have our state, and we have our JSX that we're rendering.