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

Creating a Component

Tutorial: Creating a Component

In this tutorial, you’ll learn how to create a React component using ES6 and JSX. React components are the building blocks of modern UI development, enabling reusable and modular design. By the end of this lesson, you’ll have built your first React component and integrated it into your project.


What You Will Learn

  1. Setting up a new component file in your project.
  2. Creating a React component using ES6 class syntax.
  3. Importing and rendering the component.
  4. Fixing common issues like invalid JSX and reserved keywords.

Creating Your First Component

  1. Set Up a Component Folder

    • Create a components folder inside your src directory.
    • Add a new file named Portfolio.js to house your component.
  2. Write the Component
    Open Portfolio.js and start defining your component:

    import React from 'react';
    
    class Portfolio extends React.Component {
      render() {
        return (
          <div className="portfolio">
            <h1>My Portfolio</h1>
            <p>This is a placeholder for portfolio content.</p>
          </div>
        );
      }
    }
    
    export default Portfolio;
    

    Key Points:

    • Use the class keyword to create a component.
    • Extend React.Component to inherit React functionality.
    • The render() method must return JSX.

Importing and Rendering the Component

  1. Import the Component
    Open your main client file (e.g., index.js or App.js) and import the new component:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import Portfolio from './components/Portfolio';
    
    ReactDOM.render(
      <Portfolio />,
      document.getElementById('root')
    );
    
  2. Run Your Application
    Start your development server:

    npm start
    

    Open the application in your browser to see your Portfolio component rendered on the page.


Common Issues and Fixes

1. Invalid JSX

JSX syntax requires all elements to be properly closed.

  • Self-closing tags like <img /> or <hr /> must include a /.

2. Reserved Keywords

The keyword class is reserved in JavaScript. Use className instead for JSX:

<div className="portfolio"></div>

3. Exporting Components

Ensure your component is exported using export default. Without this, your component cannot be imported elsewhere.


Refactoring HTML to JSX

If you’re moving HTML into JSX, ensure:

  • All tags are closed properly.
  • Reserved keywords like class are replaced with className.
  • Inline styles are converted to JavaScript objects:
    <div style={{ color: 'blue', fontSize: '14px' }}>Text</div>
    

Summary

Congratulations! You’ve created your first React component using ES6 and JSX. This foundational step sets the stage for building more complex and reusable components.

In the next lecture, we’ll explore additional React features to enhance your components and workflows.

Video Transcript

00:00In the last lecture we took a peek at JSX,
00:03but it's time for us to jump into React.
00:06And how about we'll start
00:07with creating our very first component.
00:10So in this lecture we're gonna be building an ESX class
00:13and with it building a component, we're gonna use JSX.
00:17So how fun is that? Let's jump right into it.
00:21Let's continue our journey into the world of components,
00:25JSX component of React.
00:28And on top of that also we're gonna
00:29continue learning ees six.
00:31So I went ahead
00:32and created a new components folder inside of our source
00:35and added an empty portfolio dot js file,
00:38which is gonna be our main place
00:40where we're gonna be working now.
00:42So let's jump right into that file
00:43and let's start developing.
00:45So I'm gonna be right inside of our portfolio file
00:48and you, we already saw the process of importing
00:51and we imported the React library that way.
00:54And I'm just gonna go ahead and set there to load up react.
00:58Now on contrary, inside of our client folder
01:02where we imported both React
01:04and the React dumb, in this case we're only gonna react,
01:08only gonna load import react.
01:10The reason is is because we wanna make it agnostic like
01:13we're talking about the, we don't want anything
01:15that is client related in here.
01:17While React DOM is a client related library
01:20that renders things inside of our library, un contrary to
01:24that, the portfolio itself doesn't need to know
01:28how it's going to be rendered
01:29and with what it's going to be rendered.
01:31Generally speaking, we have a virtual dom here really going
01:35on this is that virtual dom
01:37and it's go, it is in charge
01:38of actually doing the actual rendering while the React
01:41libraries are actual the elements themselves.
01:44Okay, so with that said, let's go ahead
01:46and start learning new ES six things.
01:50So first of all, class completely new
01:53and the class itself, portfolio portfolio.
01:59Uh, so the class portfolio, literally all I have
02:01to do is open and close that bracket.
02:02I've just created a class instead of using prototype,
02:06I could also extend the class itself by typing extends.
02:10And then I would put into here, what am I extending?
02:12In this case we're gonna be extending react component,
02:15which is an element that is gonna come from a React.
02:19It's gonna be a class instant of the component for React.
02:23The next thing really cool is that I don't have
02:25to type your function, but instead of
02:26that I just type render
02:28'cause I need to create a function called render.
02:31So I'm just typing render,
02:32open close bracket and there we go.
02:34I've just created the render function without
02:36needing to write function.
02:38How fun. Saving time is always, always great.
02:41And last but not least, we're going to send your a return,
02:43which is what each component expects in React.
02:46And I could send your org JSX
02:49or regular h uh, regular component bill,
02:53but we're not gonna even show you that process.
02:54We're gonna show it to you right away in JSX.
02:57Now what, what, what I'm gonna do is I'm gonna go
02:58Into my HTML
03:00and I'm gonna search for my portfolio
03:05portfolio, which you'll find there.
03:07Our grid section there. Here is our portfolio.
03:10What I'm gonna do is I'm just gonna grab the whole thing,
03:14the whole portfolio, the whole section,
03:16just gonna cut it out, save my HTML.
03:18Now it doesn't have a portfolio anymore
03:20and in my portfolio I'm just gonna paste it right there.
03:23All of that XML.
03:25Now if I'm just gonna go ahead and,
03:27and run my application, which just by going to the browser,
03:30you'll see that it's not going to work.
03:32Now the reason why it's not gonna go work, it's twofold.
03:35One is that we didn't actually call it,
03:37but two is that this XML is not actually valid,
03:39but let's go ahead and actually render it
03:42as if it was valid, assuming that it was valid.
03:44Here for a second I'm gonna go into my client
03:48and I'm gonna go and import another library.
03:50But this time around the library
03:52that I'm gonna import is gonna be the portfolio portfolio
03:57and it's gonna be from,
03:59and I'm gonna set the destination of where it's from.
04:01In this case it's from the current folder and side.
04:04Go one more folder into components into portfolio
04:08the file, lowercase, try to keep my files in lowercase just
04:13to keep with conventions of node in general.
04:15Alright, so now that I've imported it,
04:17I still have a problem where it's not going to work
04:19and I have your two problems.
04:21So if I just try to run it, it's not gonna work.
04:25The reason why it's not gonna work is twofold.
04:27One is I've never exported anything out of the portfolio.
04:30So everything living inside here is absolutely private.
04:34Now it's very easy to fix that right before my class.
04:37You just want to type your export
04:39default and set your class.
04:42Now what that's gonna do is gonna make sure
04:44whenever we call to import,
04:45it's gonna default export this class without a name really.
04:49And the name itself is defined here.
04:51So I could have called it something else if I've wanted to.
04:53In this case it works perfectly for me
04:55to continue calling it portfolio.
04:57So that's the first issue that we had.
04:59Now the next issue that we have is
05:02that our XML is not valid.
05:04So we wanna go into our XML
05:06and I'll, I already know what's invalid, any place
05:08that we don't close our XML.
05:10So for example, our HR needs to be self enclosed,
05:14our images need to be self enclosed as well.
05:16So let me just, uh, um, word wrap everything so it's easier
05:20to see what's going on here.
05:21So I'm just gonna go through our images
05:23and go through any place where I see an item that is not
05:26actually, um, rendered correct,
05:30well does not creating valid XML.
05:33Okay, this is good. And let me just save this.
05:36And I notice another, something I'm using here,
05:38the keyword class, which is a reserved keyword in CS six,
05:41that is the word that we use to create classes.
05:43Not allowed to use that either. So I'm gonna have
05:46to change my class from class to class.
05:48Name and class name.
05:50Uh, JSX knows automatically when you say class name,
05:53you actually mean React knows that you mean that you want it
05:57to be the class of that element.
05:59And I'm just gonna go ahead
06:00and replace all of the classes, making sure
06:05that I don't get any errors.
06:06And by the way, because we're using the React module, uh,
06:09the React, uh, for Babel, the Babel React pr, uh, preset,
06:14um, it's not gonna throw an error,
06:16but it's just not gonna actually put the class
06:18where it is supposed to be
06:19because React is expecting for a class name and not a class.
06:23This is true with other reserve names such as four.
06:26Um, and you would use four H TM L
06:28but in this case we only have classes
06:30and we are almost done
06:35and we are almost done and we are almost done.
06:39So we went through the whole process
06:40and changed all of them to class name.
06:43All that left me to do is to actually render it.
06:45So instead of rendering a diviv, I'm gonna go ahead here
06:48and get rid of this
06:49and instead of that I'm gonna type Port Folio uppercase.
06:53Now notice the difference all until now,
06:55I use lowercase everything.
06:57And even before we see this,
06:58or maybe while we see this,
07:00I'm gonna go and show you something.
07:01So inside of, uh, when I type in, in my babble browser,
07:06so we can see what's going on, you'll see
07:08that when I type XML
07:10and my react is open, it's gonna create an element,
07:13but if I type with an uppercase X, it's gonna send in
07:17that class of XML when it's creating the element.
07:20So in this case, because I set it to be an uppercase,
07:23it's gonna send in that class
07:24that we've just set up, we've just imported.
07:27So if I go ahead here and I click here on, uh,
07:29if I don't even have to click anything,
07:31if I go into my application just to look at what's going on,
07:33first of all, let's see if we have here an error.
07:35So we're having an error while compiling.
07:38And you can see we didn't find the file,
07:40what file we didn't find.
07:41We didn't file our component source.
07:44So it is basically telling us
07:45that it cannot find our components portfolio.
07:48So let me double check in my paths here if I
07:51typed everything correctly.
07:53So I have your components
07:57and I must have had a small typo there.
07:58Let's see if it's now running. And there we go.
08:01And we have your portfolio
08:02and our portfolio has been rendered.
08:04So there we go. We've created our very, very first component
08:08and you know that we're gonna go deeper, deeper, deeper.
08:11In the next section.
08:14In this lecture, we created our first ES six JSX component.
08:18Woo-hoo. Congratulations.
08:20In the next section, we're gonna jump into that wonderful
08:23for world of react as we explore
08:26and understand what what's really new about React
08:29and what's so cool and why is it so trending
08:31and hot in the industry today.

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!