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.


    Avoid Deprecated React APIs with React.StrictMode


    React has deprecated some APIs in favor of new approaches that will better support the future, concurrent model for rendering. These deprecated APIs will eventually be removed, so it makes sense to avoid them in new code and move away from them in existing code. In this lesson, we’ll wrap our boilerplate application in the StrictMode component and see it in action.



    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: I've added this componentWillMount method to this App component. Let's see what happens when we do an npm run lint.

    We're going to see that when we run lint against this, we're going to get two errors. One is for the unexpected console statement. The other one is telling us that componentWillMount is deprecated since React 16.3. This is coming from a React ESLint plugin. It is telling us that we can use unsafe as a prefix for this to clear up this linting error.

    Let's get the terminal out of the way. Let's prefix this with that unsafe_ and save it. Now if I do another npm run lint, it'll fail again, but this time just for the console statement. As far as our React code is concerned, the linter is happy with it.

    To make sure that we don't have something like this buried deep in our component tree that will prevent us from using future features, I'm going to go into the index.js file. I'm going to update this. I'm going to actually wrap this entire App component in a component called React.StrictMode.

    Now that we've added StrictMode, let's see what happens. I'm going to come in here. I'm going to do an npm run dev to run this in a browser.

    Everything looks fine in our application. I'm going to expand the dev tools. When we look at the console, we're going to see our console that is in our componentWillMount. We're going to get this warning that an unsafe life cycle method was found.

    StrictMode is going to give us these warnings for anything that's in our tree that's been deprecated. The other thing that's nice about this is that this only happens in development mode. If we go back to our code and we stop dev mode and I do an npm run build to get a production build of this application, I can open that from dist.

    Now when I look at the console here, we're still going to get our console.log from our componentWillMount life cycle hook, but we're not going to get that warning anymore. That warning is strictly for dev mode.

    Now if I come back here and I go in and I remove this and then I do an npm run dev again, we'll see that our application still works. When I look in the console, we don't have any logs or any warnings.

    This component, as long as it doesn't find any issues down the tree, it isn't really going to have any impact on our output. Now we can rely on this React.StrictMode component to warn us if anything within our application is using one of these deprecated life cycle methods.