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

Getting to Know JSX and Babel

Getting to Know JSX and Babel

In this tutorial, we will explore the basics of JSX and Babel, two fundamental tools that make working with React smoother and more intuitive. This tutorial will help you understand how JSX simplifies creating user interfaces and how Babel transpiles this code into JavaScript that browsers can understand. This content was created in 2016, and some critical updates are noted as comments to reflect modern best practices.

What is JSX?

JSX is a syntax extension for JavaScript, which allows you to write HTML-like code in your JavaScript files. It makes your code more readable and intuitive by blending HTML tags with JavaScript logic. Instead of creating elements with React.createElement(), JSX enables you to use syntax similar to HTML.

Here is an example of JSX:

import React from 'react';

const element = <h1>Hello, world!</h1>;

In this example, we create an h1 element using JSX, making the code cleaner and easier to understand.

Critical Update (2024): Modern React encourages the use of functional components with React Hooks for managing state and side effects, as opposed to older class components.

How JSX is Transformed

Browsers do not understand JSX directly, as it is not valid JavaScript. Therefore, JSX must be transpiled into JavaScript code that the browser can execute. This is where Babel comes in.

Using Babel to Transpile JSX

Babel is a JavaScript compiler that converts modern JavaScript and JSX syntax into a form that browsers can understand. This makes it possible to use the latest features of JavaScript and React while maintaining compatibility with older browsers.

Here’s how Babel transpiles JSX into JavaScript:

JSX:

const element = <h1>Hello, world!</h1>;

Transpiled JavaScript:

const element = React.createElement('h1', null, 'Hello, world!');

This transpiled code is what the browser will eventually execute.

Setting Up Babel

To work with JSX, you need to set up Babel in your development environment. Below are the steps:

  1. Install Babel Dependencies

    • You need to install Babel and its presets to transpile JSX. Use the following command to add Babel to your project:
      npm install @babel/core @babel/preset-react babel-loader --save-dev
      
  2. Add Babel Configuration

    • Create a .babelrc file in the root directory of your project to configure Babel:
      {
        "presets": ["@babel/preset-react"]
      }
      

Critical Update (2024): Many modern React setups, such as Create React App or Vite, come pre-configured with Babel and Webpack, eliminating the need for manual setup.

Practical Example: Using JSX with ReactDOM

To render a JSX element, you need to use ReactDOM. ReactDOM is responsible for rendering your React components to the actual DOM in the browser.

Here is an example of how to use JSX with ReactDOM to render an element:

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

const element = <h1>Hello, world!</h1>;

ReactDOM.render(
  element,
  document.getElementById('root')
);
  • ReactDOM.render() takes a React element and renders it to the DOM, in this case to the element with the ID root.

Critical Update (2024): The ReactDOM.render() method has been replaced with React 18’s createRoot() API for improved performance and features like concurrent rendering.

Conclusion

This tutorial covered the basics of JSX and Babel, providing you with the foundational knowledge to start building React applications. JSX makes your code cleaner and more intuitive, while Babel helps convert that code into something the browser can understand.

As React has evolved since 2016, consider using modern tools like Create React App, Vite, and React Hooks to streamline development and make your codebase easier to manage.

For more tutorials and to continue your learning journey, visit the full course at 02geek.

Video Transcript

00:00We're gonna start with our first lecture
00:01as we jump into the world of React.
00:04In this first lecture we're gonna learn about
00:05how do you actually create elements in React And
00:08with it we'll create our first element
00:10and we're gonna render that first element onto the browser
00:14and also talk about how do you pass
00:16CSS properties into this HTML that is rendered by React.
00:20So let's jump right into our first lecture in React.
00:24In this lecture we're gonna go ahead
00:26and integrate two layouts I've created for zero to geek.com.
00:29One is a future login for our site
00:32and a registration, a simplified registration page.
00:35Now we're gonna make them a lot more dynamic using react js.
00:40So go ahead and go onto the React website.
00:43It's added also to the links that are
00:45provided to this course or just do a Google search
00:47or any search for that matter for react js
00:51and click on to download the latest version of React.
00:53Currently as we're doing these lectures,
00:55the latest version is 15.1.
00:58Once you download and unzip it,
01:00you will find an examples folder that I urge you to explore
01:04and the build folder for our course,
01:06we're just gonna need the build folder, so make sure
01:08that you copy that folder
01:10and place it in your source files, in our case,
01:13in our React folder botso in the project folder
01:16that we'll create in a later lecture.
01:18With that said, now that you have all
01:20of these files configured
01:22and set, it's time for us to start exploring.
01:24You'll find in the in our React folder
01:27three HTML files are login
01:30and register, which are those templated pages
01:32that we've just seen, a empty index, HTML file,
01:36and a app js file, which is currently empty.
01:40So let's go ahead and go right into there
01:42and you'll find our index html is relatively empty,
01:46all it really has is a low.
01:47Now what I want to do is I wanna show you the steps involved
01:50now that we've downloaded all the files,
01:52the steps involved in actually creating a React JS
01:55application, the basic steps.
01:58So step number one is we need to figure out
02:00where do we want our React application to live.
02:03In this case, I'm just gonna set here a div
02:05and define it with a an id.
02:06It doesn't really matter what the idea is,
02:08but we're gonna actually use it when we're starting to work
02:11inside of our JavaScript to tell React js
02:14that this is the the canvas, this is the area
02:16where you have control over.
02:18We could have given a full control over everything.
02:21In this case we're just setting a specific area
02:24and in this specific area, we're defined area for React js.
02:27Now that we've done that, let's go ahead
02:29and note to load in the basic scripts that are needed.
02:32And I'm gonna go ahead here
02:33and I'm gonna set my source to be,
02:35and lemme just open it up here for a second.
02:37I wanna set my source to be the file
02:39that's inside of our build.
02:40Really, there's quite a lot of files here,
02:42but the two files that I'm concerned
02:44with are the React JS file and the react dom file.
02:49Now we're gonna work with the non minified version
02:51so we could get more detailed
02:54feedback from the system once we go into
02:56production in these type of files. If we're not using
02:59JSX, which we'll see in the next lecture,
03:01then we would want to use the minified versions.
03:03Alright, so let's go ahead
03:05and let's just integrate those two files.
03:07So I'm gonna go ahead here
03:08and first of all connect our build our file to react
03:13js and let's go
03:18ahead here and just add this oof r React dom.
03:22Now the separation is important to notice that contrary
03:25to a lot of other libraries,
03:27and really this is one of the biggest features
03:29of React js is the logic involved in preparing
03:32to render your data happens in the React library itself.
03:36Where it actually ends up rendering really could change in
03:40this case because we're rendering it on a browser,
03:42then we're gonna use the react dom.
03:44Now this is a really powerful feature
03:45that we're gonna talk about more throughout the lectures,
03:48but really what React JS does is it makes sure
03:51that the thing that JavaScript is the worst at,
03:53which is re rendering things,
03:55it does it the least amount of it possible.
03:58In other words, a react, make sure
04:00that it only calls the DOM when it's absolutely necessary
04:04managing all these stuff behind the scenes
04:06so you don't have to do it yourself.
04:08All right, so with that said, let's go ahead and note.
04:09So integrate our app js and I'm just gonna go ahead here
04:14and just add Oso, our app js
04:16and it's time for us to start coding.
04:18Now notice also that I've placed all these scripts in the
04:21end of our body and not on the top
04:23because I wanted to make sure that once we get here, first
04:26of all, this DOM element exists
04:28and also that if anything else that needs to render,
04:30we'll render before all the JavaScript itself is loaded.
04:33With that said, let's go into our app js
04:35that currently is empty
04:36and let's create our first react element
04:40and we're gonna go ahead and create this element
04:42by just creating a basic H one element
04:45by calling the react object
04:47and asking it to create an element.
04:49Now it has pre-built already,
04:50all these different DOM elements.
04:52So all you have to do is just send the name of the element.
04:55I'm gonna send your null for the second parameter here
04:57for a second, and the next parameter is
05:00the children of this element.
05:02In this case the children will be hello world.
05:06In this case we want to create an H one tag
05:08that its child is hello world
05:10or another words, whatever is inside here is the child of
05:14that H two H one element.
05:16Alright, so with that said, we have only one more thing left
05:19to do is we also wanna basically tell the react dom
05:22that it needs to actually render
05:24because we are telling React dom
05:26that we're gonna be using it as the rendering engine.
05:29So I'm gonna go ahead
05:30and send that H one that we've just created
05:33and I'm gonna go into my document, do get,
05:36lemme just correct this get element by id
05:43and I'm gonna go ahead and send that ID that I've created
05:45inside of our HDML, which was our app, our diviv,
05:50which is ID of app.
05:52So lemme go ahead and save this.
05:54Lemme go ahead and save this. And if we go right
05:56Back into our browser
05:58and click your on refresh on our element,
06:00we should now see hello world.
06:02So there you go. The most simple, simple,
06:04simple application I wanna show you just one more thing
06:06before we wrap up this topic
06:08and we really move into the heart of the features
06:10of React js in the following lectures.
06:13I want to go ahead and just show you one more thing
06:15that this null element
06:16that I sent here is the properties of the element.
06:19Now one property that you can't just send,
06:21you could send here a lot of properties such as data,
06:25and if I save this and go back and refresh
06:28and we'll go into our element
06:29and click here to inspect that element.
06:33Notice how we have here now a data of hey,
06:36so it automatically passes the data that attribute
06:39that we've added as this property.
06:41That is an object. I cannot set a class
06:44because in E ES six the class word is reserved.
06:47So instead of using class, we'll use class name
06:50and define the name of the class that we want to assign.
06:52Let's say in this case, I wanna mark it as success,
06:55which is a class I already have pre-configured in the CSS
06:58of our application.
07:00So if I go here and I click here on refresh, you'll see that
07:02that class is added In this lecture we created our first
07:06React application
07:08and in the next lecture we're gonna go ahead
07:09and learn about JSX
07:11and how to integrate babble into our application
07:15and learn what it actually is.

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!