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.


    Configure babel for React with preset-react


    In this lesson, we'll add React and ReactDOM to our project and create an entry point for the React application. We'll also configure Babel with the React preset so Babel can transpile our JSX into JavaScript that will run in a browser.



    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: Right now the entry point for this application simply logs something out to the console. Instead of doing that, I'd like this to be a React application. In order to do that, we need to install some new dependencies. In the terminal, I'm going to do an npm i, this time passing it the -S flag to save as runtime dependencies.

    We're going to install React, ReactDOM, and the prop-types library. With those installed, lets go back into our app, and in the source directory, I'm going to add a new file. I'm going to call this app.js. This is going to be my top-level React component. Here, I'm going to import react from React, and I'm going to define a class component called F.

    That's going to extend react.component. Then I'll expose a render method that for now is just going to return an h1 that says hello world, and then down at the bottom of the file, we're going to export default app. Then we can save the app file.

    In index.js, I'm going to get rid of both lines of code, and I'm going to import react from React. I'm going to import react-dom from ReactDOM, and I'm going to import app from the relative path to our app module. I can drop down here, and I can call react-dom.render.

    I'm going to render the app component into a DOM node that we'll get by ID. We'll call that app. Let's save index.js, and we're no longer using greet.js, so let's go ahead and just delete that file. Now, in the terminal, I'm going to run npm run build. We're going to see that we get some errors when we try to build this.

    If we scroll up a little bit, we'll see that it's failing with a syntax error, an unexpected token where it first encounters our JSX. JSX is not valid JavaScript. When we run this through the Babel loader, it doesn't know what to do with this code. We need to tell Babel how it can handle JSX, and transform it into valid JavaScript.

    We're going to do that with another preset. We're going to npm i -d, because we're going to save it as a dev dependency, @babel/preset-react. With that installed, we can go into our webpack config, and where we have our Babel loader options, and we're specifying our preset env, we can add to that array.

    We'll do @babel/preset-react, and we'll save our webpack config. Then back in the terminal, you can npm run build. This time, we'll see it's successful, and we get our app.bundle.js file emitted. If we go into our dist directory, and open that file up, even though it's minified, we'll see that it's far longer than it was before.

    This is because React and ReactDOM are both part of this bundle now.