02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Hello JSX

Tutorial: Setting Up and Using JSX in React.js

In this tutorial, you will learn how to set up JSX in your React.js project and understand its core functionality. JSX is a powerful syntax extension for JavaScript that makes building React components more intuitive and maintainable.


What is JSX?

JSX (JavaScript XML) is a syntax extension for JavaScript. It enables you to write HTML-like elements directly within your JavaScript code. These elements are then converted into React-specific JavaScript calls during the build process.

Key Features of JSX:

  • Combines HTML structure and JavaScript logic in a single file.
  • Makes your code more readable and maintainable.
  • Works seamlessly with React's component-based architecture.

Steps to Set Up JSX in Babel

  1. Install Babel JSX Preset:
    Add support for JSX in Babel by installing the @babel/preset-react package.

    npm install --save-dev @babel/preset-react
    
  2. Update Babel Configuration:
    Add the react preset to Babel's configuration file (e.g., .babelrc or babel.config.js):

    {
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
    
  3. Install React and ReactDOM:
    Install React and ReactDOM to handle React components and rendering:

    npm install react react-dom
    
  4. Update Webpack Configuration:
    Ensure Webpack processes JSX files correctly by including Babel Polyfill in your entry file:

    entry: ['@babel/polyfill', './src/index.js']
    

Basic Rules of JSX

  1. Single Root Element:
    JSX must have a single root element. For example:

    return (
      <div>
        <h1>Hello World</h1>
        <p>This is a paragraph.</p>
      </div>
    );
    
  2. Close All Tags:
    All elements must be properly closed, even self-closing ones:

    <img src="example.jpg" alt="Example" />
    
  3. Embed JavaScript Expressions:
    Use curly braces {} to embed JavaScript expressions within JSX:

    const name = 'React';
    return <h1>Welcome to {name}</h1>;
    

Rendering JSX to the DOM

To render JSX to the DOM, use the ReactDOM.render() method:

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  <div>Hello JSX!</div>,
  document.getElementById('root')
);

Advantages of JSX in React

  • Unified Code Structure: Combines HTML and JavaScript logic in one place.
  • Improved Maintainability: Easier to track and update components.
  • Enhanced Debugging: Clearer error messages due to JSX's structure and syntax.

Summary

In this lecture, you configured Babel to support JSX, installed necessary dependencies, and explored JSX's basic syntax and usage. This setup provides the foundation for building React components efficiently.

In the next lecture, we’ll take this knowledge a step further and create our first React component using JSX.

Video Transcript

00:00So far we've integrated ES six.
00:02So now we could use ES six in our applications,
00:05in our JavaScript, but we can't use JSX yet.
00:09So in this lecture, we're gonna sort that out
00:11and we're gonna install JSX support into Babel.
00:15Beyond that ROT are gonna look at the basics of JSX,
00:18what is it, how to work with it.
00:20So let's jump right into it.
00:22Alrightyy, we're finally ready to talk about React.
00:25And to do that we really wanna,
00:27and I'm gonna go back into our,
00:28and let's just clean this out now.
00:31I'm gonna install the React plugin as well,
00:33or the React preset as well into Babel in a second.
00:37And the reason is, is
00:39because the React, it's really cool, it has something
00:42that's called JSX.
00:44JSX is an extension of JavaScript
00:46that really works really perfectly with React,
00:49the whole pre premise of React.
00:51And one of the main premises is, is to put all
00:54of the actual component logic,
00:57which is basically what we're doing.
00:58We're gonna be taking all of the visual logic,
01:02including the actual Villa Vi visuals
01:06and bundling up together into one file.
01:08Why is that such a powerful thing?
01:10Because it gives us easy access to all the things
01:12that we need access to, all the things that we're planning
01:15to manipulate, all the things that we want
01:17to have control over, contrary
01:19to building traditional HTML files, traditional CSS files,
01:23traditional script files where everything was separate
01:27and in different places, making it completely unusable.
01:30So to be able to make things more usable
01:32and easier to program, the concept was
01:35to integrate into JavaScript XML.
01:39Now, the idea itself is very simple, is
01:41where if you send in XML,
01:44and it has to be, it has to have a root only one root child.
01:48So basically, if you, if I have more than one,
01:51if I have something, I have to close it.
01:53I would never have two childs at the same time.
01:55But its content would be something inside there.
01:57So there's, let's say HTML
02:01and maybe body just
02:04for the sake of just, it doesn't really matter.
02:05It doesn't need to be valid. HTML here.
02:07What's really important
02:08that your element will be just one element
02:11whenever you're defining XML elements,
02:13that is really the core logic, the, the core thing
02:17that you have to have configured and set up.
02:20Now with that said, we're going to look, see it in action.
02:22That is just the starting point of how you create things
02:25and react js.
02:27But let's go ahead and start actually installing
02:29all the things that we need.
02:31So to do that, I'm gonna go back into my terminal.
02:33And again, I'm gonna have to break out of our server.
02:36Um, by the way, all this, oh, I, I use command, uh,
02:39or control C to get out of the server.
02:41Um, that's what I do.
02:42So now the next thing we want do, again, we wanna go
02:44to NPM install.
02:46We want to save it into our dev,
02:48and we want to install Babel preset React.
02:52'cause that's gonna give us access to
02:53that really cool hybrid XML in our JavaScript
02:57that gets rendered down
02:59to ES five once the installation is complete.
03:02So while that's still going on
03:04and while that's installing it, I'll jump back in
03:06for a second into my browser to show you that.
03:08You could see on this end, this is
03:10how it's transcribed into E ES five.
03:12And this is just regular react code.
03:14So obviously you have to have Oso React installed,
03:16which we'll do next.
03:18But the e the JSX part,
03:21we're actually exporting in the dev.
03:23Why in the dev, because we are, we don't want it
03:26to happen in the runtime while the application is running.
03:28We want it to convert our JavaScript file
03:31before anything runs, before even our server does anything.
03:35After our server runs, we're gonna need access
03:37to our React library.
03:38We don't want to decompile
03:39or any change anything inside of that library.
03:42And in that case, we want
03:43to actually install it into our actual production files.
03:46So let me just check we're, if we,
03:48our preset is done installing or it's still working there.
03:51Okay, it's done. Beautiful. Let's continue on installing.
03:55So NPM install,
03:57but this time for the first time, we're gonna go ahead
04:00and save it into our actual real environment
04:03and into our real environment.
04:04I want to install a few things.
04:05First, I want to install React.
04:07The next I want to install react dumb.
04:10Now react, react as the
04:15internal logic while React Dom is
04:17what actually is managing the actual rendering itself
04:20onto the actual screen.
04:22So we have the module itself
04:23and the separation makes it possible for
04:25to have different libraries doing the, the rendering.
04:28In this case, we're rendering it on the client side.
04:30So we're using React and we're using React.
04:33Dumb last but last least last
04:35but not least, if we're already installing things into our
04:37production area, I also wanna install Babel polyfill.
04:42Now, bale polyfill, what it actually does.
04:45And by the way, by the way, polyfill also has a similar
04:47name, which is called uh, shim, which basically is basically
04:51to help older browsers that don't support ES five
04:55but have partial support to get the full features
04:58that are available in E Es five.
05:00So what Babel Polyfil will do is add into our production,
05:04basically access O2 to our poly fills,
05:07basically making e our uh, older browser like IE eight,
05:11have access to things that are in ES five
05:14that it doesn't natively have.
05:16Now to make it work, I'm just gonna go ahead, even
05:18before we continue with our modules,
05:20I'm gonna go into our config of our web pack
05:22and I'm gonna change my entry from being a
05:26string into an array
05:27because what I wanna do is I also want to include
05:30that Babel polyfill.
05:32So to do that, I'm literally just gonna go ahead
05:34and type your Babel polyfill
05:39and that's gonna make sure
05:40that it's also gonna work on older browsers.
05:41Really cool. We don't have to worry now about any support.
05:45Oh, 'cause we know we're working on ES five
05:47and we're also extending support to older browsers.
05:49Incredibly cool. So that's all we had to do
05:52to configure and set things up.
05:54Now the next step that I want to do is to, now
05:56that everything is, is installed
05:58and we installed into our dev dependencies, our new presets
06:02of React, I want to go ahead
06:03and make sure oso that I'm adding that into my preset.
06:06So making sure that I'm sending it in, in my loaders,
06:09making sure that, uh, we'll be able to start typing in JSX.
06:14And last but not least, again, we said we set our polyfill,
06:17we set up react, we set up Dom.
06:19Now with that said, let's go ahead into our client.
06:21And what I wanna do on my client now is I'm gonna go ahead
06:24and show you how do you import things in E es six.
06:28Now the way you import things in E ES six is incredibly
06:30easy and cool.
06:31You just type import
06:33and you type the name
06:34that you want your library to be called.
06:36This is not the name that is
06:37necessarily inside of the library.
06:39This is any name that you want really.
06:41And then you say from where and you set the path.
06:44In this case, our path is React.
06:47React J js is sitting somewhere inside of our modules,
06:50node knows automatically to recognize it,
06:52and so does Webpack, which will package it
06:54and send the right file in.
06:57So once IP uh, I'm importing React, I can, uh,
07:01I wanna show you the process of how do you actually put
07:04and update something on your stage.
07:05So I'm gonna go ahead and also import React dom,
07:09and that's that I'm gonna import it from React dom.
07:13Okay? So I have the two files that I need to import
07:15to be able to render things.
07:17The next thing that I want to do is I'm gonna go ahead here
07:19and maybe after this, hey, instead of that, let's go ahead
07:22and send here a return, uh, return.
07:26And we're just gonna return this value.
07:29And instead of that, let's go ahead
07:31and I'm gonna go to my React dom.
07:33React dom, and I'm gonna go ahead do render
07:39and I am going to render a very simple
07:44diviv that has inside of it.
07:47Hello, from inside here.
07:52And I'm gonna go ahead
07:54and send the next item, which will be,
07:55and let me just open up my document.
07:57I need to find somewhere in my document that I could,
08:00I have yours some sort of an idea.
08:01Here we go. We have Body page top. Beautiful.
08:04So I'm gonna take that
08:05and just override everything inside of the page.
08:07So again, in my client itself, I'm gonna ask for my doc
08:10as my second parameter document.
08:14Do get Element by id.
08:19And I'm gonna send in that element and let me just word wrap
08:23and let's give it a run.
08:25So first of all, let me start up my server
08:27'cause it should be down now
08:28because we did all those installations
08:33and our server can, uh, completes that packaging process.
08:36It will automatically refresh our browser. And here we go.
08:39We have hello from inside here, even though we used actually
08:44XML directly inside of our client.
08:46So that is really the basics of how you work with JSX.
08:49But it's just the beginning.
08:51And in the next lecture, we're gonna take it
08:53a step much deeper
08:54As we create our very first component in this lecture,
08:59we set up JSX.
09:00And so the basics of how it works, it's time for us
09:03to turn our attention into the next lecture
09:05to actually creating a component that uses JSX.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!