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

Creating an ES6 React Component

In this tutorial, we explore the creation of an ES6 React component, leveraging ES6 classes and JSX, while learning how to import and export modules in Node.js. By the end, you’ll understand how to write reusable, maintainable React components and separate logic for clean and modular application development.

1. Introduction to ES6 and React Components

React components are the building blocks of a React application. This tutorial introduces you to creating components using ES6 class syntax. ES6 classes provide a structured way to define components, ensuring reusability and readability. Additionally, we delve into JSX, a syntax extension that allows writing HTML elements directly within JavaScript.


2. Setting Up the Project

We start with a project that has a client.js file containing a basic React component. The problem? The file is cluttered with both client-specific logic and component definitions. To resolve this, we create a dedicated App.js file to house our React component, following isomorphic JavaScript principles. This separation makes the code more modular and reusable.

  1. Create a new file:

    • In the project directory, create a file named App.js.
  2. Define the component class:

    • Use the ES6 class syntax to define a React component.
    • Extend the React.Component base class to inherit React features.
import React from 'react';

class App extends React.Component {
  render() {
    return (
      <div>
        <h1>Welcome to React</h1>
      </div>
    );
  }
}

export default App;
  1. Export the component:
    • Use the export default syntax to make the App component available for import in other files.

3. Updating the Client Logic

Now that we’ve created the App.js component, update the client.js file to import and render the new component:

  1. Import the App component:
    • Use the import statement to include the App component.
import ReactDOM from 'react-dom';
import App from './App';
  1. Render the component into the DOM:
    • Use ReactDOM.render to attach the App component to the root element in the HTML file.
ReactDOM.render(<App />, document.getElementById('root'));

4. Understanding JSX in React

JSX simplifies writing React components by allowing you to embed HTML-like syntax directly into JavaScript. A few key points about JSX:

  • Use className instead of class for defining CSS classes (as class is a reserved keyword in JavaScript).
  • Inline styles must be defined as JavaScript objects. For example:
const styles = { backgroundColor: 'blue', color: 'white' };
<div style={styles}>Hello, World!</div>
  • Components are distinguished from HTML elements by using PascalCase (e.g., <App />).

5. Why Separate Component Logic?

By moving the component logic into App.js, we achieve:

  • Modularity: Each component is self-contained, making it easier to reuse across the project.
  • Maintainability: The client.js file focuses solely on client-specific tasks, while App.js handles the component logic.
  • Scalability: This setup allows seamless rendering of the component on multiple platforms (e.g., web, server, mobile).

6. Testing the Setup

After making the updates, refresh your browser to ensure everything works as expected. The rendered output should display the content of the App component without any visual changes.

If any errors occur (e.g., target container not found), double-check the id attribute in your HTML file to ensure it matches the ReactDOM.render target.


In this tutorial, we created a foundational React component using ES6 syntax and organized it into a modular structure. This approach prepares us for building more complex React applications. In the next tutorial, we will enhance our React components by adding a reusable Bootstrap button component.

Video Transcript

00:00Welcome back to a new section where we steer our attention
00:04to ES six and React components.
00:07In the process, we're gonna get to know more about ES six
00:10and how to work with it,
00:11and we're gonna build out react components.
00:13There'll be more reusable.
00:15We'll show you strategies to create really great components
00:19and we we'll have a lot of react.
00:21So let's jump right into our first lecture
00:24because in our first lecture we're gonna be creating our
00:26first E ES six react component through it.
00:30You're gonna learn about the E ES six classes
00:32and you're gonna learn how to import
00:34and export modules in node js.
00:38Technically, that's the platform that we're working with.
00:40But how you do that, generally speaking, working with
00:45ES six projects and also we'll learn a lot more about JSX.
00:49Let's jump right into doing that.
00:53Now, one of the critical things,
00:55we've created a component, right?
00:57And it's inside of our client js.
00:59It's a react component that leverages bootstrap,
01:03but the problem with our component right now is implied
01:06by its name, which is client js.
01:09In reality, a lot of the code here,
01:11mainly I'm talking about the component itself,
01:14really shouldn't live within the client.
01:17Now the reason is it really implies
01:19to this bigger picture idea in programming in JavaScript,
01:23which is called isomorphic JavaScript.
01:26Now, this term is a little bit scary,
01:28but all it really implies is that you really want
01:31to create your code and I'm creating a file in the process
01:34and I'm just gonna call it app js.
01:37And the idea itself is you wanna basically create
01:42the most minimal code you can in each file,
01:45but beyond that oto, for example, in the scenario of client,
01:49we only want client specific things inside of this folder.
01:53While this app could have been generated oats on the server
01:57if we were using instead of react dom, a different rendering
02:00or it might have been rendered even in a mobile device.
02:04So really the first idea is
02:06to extract out the actual component itself
02:09or the logic within into a main application file.
02:14Now we're gonna do that throughout this lecture
02:16and our first step that we need to do to be able to do
02:19that is actually create a class in ES six.
02:23Now, we could have done it in different ways, but
02:24because we're doing a whole ES six title about React,
02:29the process is very similar.
02:30We just type the word class,
02:33we define a name for that class.
02:35In our case, I want it to be our app.
02:37I am then gonna open and close a curly bracket
02:40to define our class.
02:43And next I will want
02:44to actually create the different methods of this class.
02:47Now, before I even do that, I'm gonna, I'm gonna want
02:51to load up the actual React library
02:54'cause I want this class to be a child of re a
02:58React component.
03:00To do that, I'm gonna type app extends
03:03and type react component.
03:06Now the component is basically the base class
03:09for all components.
03:10And what I'm actually telling my app that it is a component,
03:13it's extending that component features.
03:17Last but not least, to be able to run a React application,
03:21I also need to create the render function.
03:24And the render function inside
03:26of the class literally spells out what should be rendered.
03:30Now the most basic construct, I'll just return
03:34and then go inside of here.
03:35I'm just gonna copy that value that we've created
03:38and I'm just gonna cut it out of our client
03:41and put it right inside there as our value.
03:44Now what I've done here is I created a minimalistic very
03:47basic react component.
03:49Now because it's ES six,
03:50it's gonna automatically turn this all into, you could say,
03:54information that will be transcribed down
03:56until it turns into an actual JavaScript.
03:59Now for this to be able to work, I also need to send this
04:03to our application.
04:04Now in re, in ES six, very similar
04:08to the way we saw with Webpac, where we exported our module.
04:13This is how it's done in node in ES six.
04:16The way we announced to the world that this is,
04:19this is content that we want to export, is
04:21by using the word export.
04:23Now, there's a few different ways to export things
04:26and multiple items can be exported
04:28because in our case, we only have one class.
04:31We also need to add the words default
04:34by adding the words default to our export
04:37before our class where we're actually saying,
04:39whenever someone tries to load up this file,
04:42send them this class by default.
04:44And in other words, whenever we create an import,
04:47for example, for React,
04:48it will automatically send the React class that's living
04:51with within this package,
04:53even though this package has multiple libraries
04:56and multiple elements beyond that.
04:59So I'm gonna go ahead and save this change
05:01and I'm gonna go into my client
05:03and what I wanna do in my client, I'm gonna go ahead
05:05and click on import
05:08and gonna import our app from the current folder
05:12that we're in app.
05:14Now, I don't have to type JS
05:16because it will automatically search for the dot js contrary
05:20to the top of your right, didn't set the folder
05:22because really I didn't want it in the folder.
05:24I wanted it to look through in the node modules packages
05:27because it's one of those packages.
05:29In the case of our first source file that is externalized,
05:32we're actually approaching a local file,
05:35in this case, our app file.
05:37And now once we've done that, I'm gonna go ahead
05:39and I'm not gonna create a new Conte
05:41because instead of that, I'm just gonna go ahead
05:43and create that component directly inside an HTML tag
05:48that is self enclosed.
05:50Now the beauty here in React is JSX more accurately.
05:55Whenever I type inside, for
05:56Example, a diviv, it will not search for a class.
06:00It will automatically know that it's an HTML element
06:02that I'm trying to recreate.
06:04But if I go ahead and enter here in uppercase, it's going
06:08to expect to find a class with the name of div.
06:12In our situation, we have your class,
06:14and deliberately we set it with an uppercase letter.
06:17So React or the JSX transcriber will automatically recognize
06:22that what I wanna put inside here is not actually HTML,
06:26but instead of that I want to actually approach a class.
06:29So it's gonna go and instantiate that class
06:32and put it inside of here, really clean code.
06:35So if I go ahead and save this
06:36and make sure that I saved my app as well,
06:39if everything worked according to plan,
06:41when we go into our browser,
06:43we really shouldn't see anything change.
06:45Our content should remain the same.
06:47But the big difference that happened between what was before
06:50and now is our client is much smarter
06:53because it does not know the inner details of
06:56what is actually in the application.
06:58And if I have a rendering that is done on the server
07:00that uses the same client, I could literally call that app
07:04with a separate file such as server js
07:07and run a different logic for the actual rendering process.
07:11So I've separated now between the actual logic flow
07:15of our application, our React application,
07:17and our client itself.
07:21In this lecture, we learned how to create ES six components
07:25and also learned how to learn more about JSX
07:28and how to import and export modules in the next lecture.
07:32Now that we have the foundations in place, it's time for us
07:35to build out a reusable bootstrap React button.

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!