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

Creating My First React Element

Creating My First React Element

In this tutorial, we will learn how to create your very first React element. This is an essential step in understanding the fundamentals of React and will help you get comfortable with building user interfaces using JavaScript. This video is from 2016, and we'll provide comments on any critical updates to the methods discussed.

Setting Up Your Environment

To create a React element, you need a few essential tools installed on your system. Here’s what we cover in this section:

  1. Install Node.js and NPM

    • Visit nodejs.org to download and install Node.js, which comes bundled with NPM (Node Package Manager).

    • After installation, verify your versions by running:

      node -v
      npm -v
      

    Note: As of 2024, modern setups might include tools like Yarn or PNPM as an alternative to NPM for better performance and dependency management.

  2. Install React and ReactDOM

    • To get started with React, you need to install React and ReactDOM.
      npm install react react-dom
      

Creating a Simple React Element

Once you have your environment set up, you can start creating your first React element. React elements are the building blocks of React applications, and they represent what you see on the screen.

Here's how to create your first React element:

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

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

ReactDOM.render(
  element,
  document.getElementById('root')
);

Explanation

  • React.createElement(): This function creates a new React element. In this example, we are creating an h1 element that says "Hello, world!".
  • ReactDOM.render(): This function takes the element and renders it to the DOM. Here, we're rendering our h1 element inside an HTML element with the ID root.

Critical Update (2024): Modern React has introduced JSX and React hooks as a more readable and intuitive way to create elements and manage state. Consider using JSX instead of React.createElement():

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

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

ReactDOM.render(
  element,
  document.getElementById('root')
);

What is JSX?

JSX is a syntax extension for JavaScript that looks similar to HTML. It makes it easier to write and understand the UI code.

Using JSX

Instead of using React.createElement(), JSX allows you to write HTML-like code inside your JavaScript. The example above using JSX looks like this:

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

Note: Browsers do not understand JSX directly, so it's transpiled by tools like Babel before being rendered.

Setting Up Babel

Since JSX is not native JavaScript, we need Babel to transpile it. Here’s how to set up Babel in your project:

  1. Install Babel dependencies:

    npm install @babel/core @babel/preset-react babel-loader --save-dev
    
  2. Add a Babel configuration file (.babelrc):

    {
      "presets": ["@babel/preset-react"]
    }
    

Critical Update (2024): Many modern setups like Create React App or Vite come pre-configured with Babel and Webpack, allowing you to skip much of the manual setup.

Rendering Your React Element

To render your element, you need an HTML file that serves as the entry point for your React application. Here’s an example:

index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>React Tutorial</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="index.js"></script>
  </body>
</html>

In this HTML file, the div with the ID root is where React will render your elements.

Conclusion

Congratulations! You have successfully created your first React element and rendered it to the page. This is a crucial step towards building more complex user interfaces in React. Moving forward, consider using JSX for a cleaner and more efficient development process, and leverage modern tools like React Hooks to manage state effectively.

For more tutorials and to continue your journey in learning React, 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!