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

001.005.Building our first JSX React Component

Congratulations on reaching this pivotal stage! In this tutorial, you’ll build your first JSX-based React component and integrate it into your project. Along the way, you’ll explore the nuances of JSX, set up React libraries, and understand how React and JSX work behind the scenes. By the end, you’ll have a functioning React component rendering directly to the DOM.


Steps to Build a JSX React Component

1. Understanding React and JSX

React components are the core building blocks of React applications. These components use JSX (JavaScript XML), a syntax extension that resembles HTML but works seamlessly with React. JSX makes it easier to visualize and manage your UI structure within JavaScript files.


2. Install React and ReactDOM

To create and render React components, you need two libraries:

  • React: Creates and manages the components.
  • ReactDOM: Handles rendering components to the DOM.

Run the following command to install these libraries:

npm install react react-dom

Note: Unlike development tools, React libraries should be installed as production dependencies (without --save-dev) because they are required when your app runs.


3. Set Up Your Component

  1. Import React and ReactDOM
    Add the following imports to your JavaScript file to use React and ReactDOM:

    import React from 'react';  
    import ReactDOM from 'react-dom';  
    
  2. Prepare JSX for Your Component
    JSX is syntactically similar to HTML. To build your first component:

    • Open your HTML file and copy the relevant markup (e.g., a Bootstrap Jumbotron).
    • Paste this markup into your JavaScript file.

    Example:

    const MyComponent = () => {  
      return (  
        <div className="jumbotron">  
          <h1>Welcome to React!</h1>  
          <p>This is your first JSX React component.</p>  
          <button className="btn btn-primary">Click Me</button>  
        </div>  
      );  
    };  
    

    Important: In JSX, use className instead of class (a reserved keyword in JavaScript).

  3. Render Your Component to the DOM
    Use ReactDOM.render() to render the component into a specific element in your HTML file.

    • Add a div with an id in your HTML:
      <div id="root"></div>  
      
    • In your JavaScript, render your component:
      ReactDOM.render(<MyComponent />, document.getElementById('root'));  
      

4. Handling Inline Styles in JSX

In JSX, inline styles are defined as objects, and property names follow camelCase conventions. For example:

const MyStyledComponent = () => {  
  return (  
    <div  
      style={{  
        backgroundColor: 'lightblue',  
        padding: '20px',  
        textAlign: 'center'  
      }}  
    >  
      <h1 style={{ color: 'darkblue' }}>Styled Component</h1>  
    </div>  
  );  
};  

5. Test Your Component

Start your development server:

npm start

Open your browser and verify that your React component is rendering as expected.


Key Takeaways

  1. JSX Basics: JSX allows you to write HTML-like syntax in your JavaScript files. It must have a single root element (e.g., a div) and use className for CSS classes.
  2. ReactDOM: This library bridges the gap between React components and the actual DOM.
  3. Inline Styling in JSX: Inline styles must use JavaScript objects and camelCase property names.
  4. Debugging JSX: Errors in JSX often relate to syntax or reserved keywords (like class or for).

Next Steps

This tutorial marks your first steps into React development. In the next section, you’ll dive deeper into creating reusable React components, managing state, and exploring ES6 features like destructuring and arrow functions.

Video Transcript

00:00Hooray, you made it to the last lecture of this section
00:03where we're actually gonna create our first JSX React
00:06component in the process.
00:08We'll get a bit more familiar with what JSX is
00:11and how it works and we're also gonna basically integrate
00:14bootstrap into this JSX React component
00:17that we're going to be creating.
00:19So let's go ahead, let's create our first component.
00:23Alright? Let's go ahead and turn our component
00:27or our visual that we've created into a React component.
00:30Now to do that we're gonna have to stop our server first
00:33because what we're gonna have to do is we're gonna have
00:35to install the react packages.
00:38Now, contrary to the past when we did an install
00:41and all the time we set it to be save dev
00:44because we need it for development in this case,
00:47we really need react to be available for our production.
00:50So to do that I'm gonna only set it to save
00:53and it's important to click on that save
00:55'cause we wanna update our package information,
00:58our JSON package, our package JSON,
01:01because we wanna be able to move our files
01:05and I'm gonna show you what I mean in in the end
01:07of this lecture.
01:08Okay, so first of all I wanna set up React
01:10and the React library comes in two separate libraries.
01:14One is the library that is in charge of rendering things out
01:19or creating the elements, which is the react element.
01:22While the other is in charge of the communication
01:25with the platform that is going to be displayed on,
01:28in our case, we want to display it on the DOM
01:30inside the HTML.
01:32So to do that we need to install React and react Dumb.
01:36Now I wanna talk about a few things while it's
01:39still installing.
01:40If you take a look at your JSON file
01:42and I'll definitely not savior anything again
01:44because we, we wanna wait for the dependencies to come in
01:46that are not dev dependencies.
01:48I wanted to talk for a minute about
01:50what do these version numbers actually mean.
01:53So for one, the small numbers mean
01:56that there's actually just fixes of bugs
01:59and things of that nature.
02:01A large number, whenever we move from two
02:03to three means a new feature was added
02:05but there's backward compatibility.
02:07We didn't break something.
02:09On the contrary, whenever we move a version from six
02:12to seven or anything like that, it's a declaration
02:15that we're gonna be breaking functionality.
02:18So when you're taking this course
02:19and you're working on stuff,
02:21you might find yourself in a situation when you're
02:23installing Babel
02:24or installing web pack that something's not working
02:27as the way you want it to work.
02:30And in that case, go back into the package, do js ON
02:33and downgrade the version to make sure that you're in one
02:36of the versions of webpac.
02:38One point something and babble is six point something
02:42and any other item that we'll work on, for example
02:44with React we're working on version 15.
02:47When version 16 will come out then some FA
02:50functionality could break.
02:52So you wanna make sure that if it does break, to go back
02:54and revert and double check to see that you're working
02:57with the same versions that were
02:58Working or you might end up in a situation
03:01where things don't work.
03:03So with that said, if the installation has completed, uh,
03:06we, if we take a look at our dependencies that we'll see
03:09that we have here the React and we have your React do
03:11and is in version 15.2 currently.
03:14But it really doesn't matter if it's 15 point a hundred
03:17as long as we're still in version 15
03:19'cause we'll know that no major functionality has shifted
03:23or changed or broken backward compatibility.
03:26With that said, let's go into our client
03:28and let's start developing and I'm just gonna go ahead
03:30and delete everything that I have there
03:32and start with the first new E ES six thing that you need
03:35to know about, which is how do you import external classes,
03:40external elements, external things.
03:43So for this case I really want to import the React library
03:46and I want to import it from the react from node,
03:50which is one of those packages and node of React.
03:52And literally that's all I have to do, react
03:57dom from React dom
04:01'cause we're gonna definitely need both packages.
04:03Okay beautiful. So now that we've imported them,
04:06and by the way, even if you don't see us using both of them,
04:09we do need both of them here and I'll explain why
04:11and that relates to our JSX
04:14that we're gonna be creating right away.
04:16So we talked about let
04:17and we said that let is similar to VAR
04:19with a slight difference
04:20and the slight difference between let
04:22and VAR is that while VARs depend on the this,
04:26that you're within a let depends only on the
04:31CLO enclosure that it's within.
04:33So if you put a let, so for example if I do a four
04:36and I put here a VAR
04:38and a VAR a, that VAR will exist anywhere inside
04:42of the same context, the same lifecycle of the application,
04:46while our let would only live within
04:48that four if we create a let.
04:51The same is true oso with constant.
04:53That's really the difference. But the only addition to cons
04:56that does not exist inside of L is that it's a constant,
05:00which means once you define it, it should never be changed.
05:03Now it does not mean that the content within cannot change,
05:06it just means you don't wanna reassign the variable again.
05:10So with that said, let's go ahead
05:11and create our component in JSX, which is crazy cool.
05:15So I'm gonna go into my HTML
05:18and in my HTML what I'm gonna do is I'm gonna grab the,
05:21our whole jumbotron with all the divs included
05:24and just snap it right out.
05:26Save our HT html so we'll be empty
05:29and in our client, I'm literally just gonna put HTML
05:32directly into our JavaScript and save it as a JavaScript.
05:37Now if I go ahead
05:39and look into my browser, I might see errors,
05:41although I'm not seeing errors
05:43and the reason, well we're not errors,
05:46but the reason why I would expect to have an error is
05:48because the word class is a reserved word in ES six.
05:51The only reason why we're not getting an error is if we go
05:55ahead and search for this line
05:57Inside of our outputted JavaScript is
05:59because Babel is transcribing our
06:02content, what's going on?
06:08Oh, I need to restart the server.
06:10Well maybe that's why we didn't get an error
06:12that would explain it as well.
06:13Okay, so server restarting.
06:15Now that the server's restarted, let's turn it back on.
06:18So first of all, we're not seeing anything which is a good,
06:20but we're also not seeing an error.
06:21As I said, we wouldn't and if we go ahead
06:24and search for that building part just to find it inside
06:28of our code, why am I not finding it?
06:33And let me just give it another shot here.
06:36Okay, so it's rebuilt our model
06:37because I didn't actually save it,
06:39which is two no-nos at one with one whip
06:43and you could see that it transcribed everything
06:46and it converted our our HTML
06:48that we put it into JavaScript, which is quite interesting
06:51and it actually converted it into a React object,
06:55which is exactly what it should do.
06:57Beyond that, if you'll notice Oso in the place
07:00where the classes are, a class is really passed through
07:03as the word class as it should.
07:05And that is because we're working with that package,
07:09which is called Babel preset React.
07:12And because of that we could type directly class instead
07:15of typing in something else,
07:17but if we weren't working with it, by the way, we would need
07:20to type your class name.
07:21Generally speaking, you probably want
07:23to type your class name
07:25and we're, you know, let me show you why
07:26because React itself expects that.
07:29And by the way for now assume JSX is just standard HTML,
07:34although it has to be legal HTML.
07:35And the basic core rules are that it has
07:38to have only one root element.
07:40So by that though, we put here only one
07:42element that has children.
07:43That's fine even if the children are multiple.
07:46Now let's go ahead and just render it.
07:48And rendering is a really simple process as well.
07:51We call the React dom render
07:53because what we're trying
07:54to do is we wanna basically reach out to the DOM itself
07:58and render out our component that we've just created.
08:01That right here is just pure HTM L.
08:03And you saw how it's being transcribed dynamically
08:06with Babel into this really more complex thing
08:10that you don't have to even worry about
08:11because all you have to worry about is that um,
08:14the second parameter, by the way I'm typing
08:15as I'm speaking is I need to put your get element by ID
08:20and fetch here the ID where we want to put it.
08:22In this case I'm gonna call it React.
08:24I'm gonna save this, go back into my HTML
08:27and I'm gonna create here a diviv
08:29and I'm just gonna call that diviv,
08:32give it an idea of react.
08:34So react knows where to place its content.
08:37So literally what's going on here is this component which is
08:40behind the scenes being created from our HTML
08:43that we've created here with the help of the React package.
08:47By the way, that's the reason why we imported it.
08:49If we wouldn't have imported it,
08:51we would've gotten an error.
08:52So JSX, what it's
08:54Doing is automatically converting it
08:56and using that package to create our component.
09:01And once a component is created, we're sending it in
09:03through the React dom library to render it
09:06and telling it exactly where we want it to render.
09:08Now if everything working according to plan
09:11and we go back into our page,
09:13we should now actually see our content.
09:15But first of all, we could see
09:16that we're having here an a small error
09:19that the target container is not a DOM element.
09:23Now the reason why we're getting this by the way, is
09:25because we're not u we're using this class instead
09:28of saying class name, oh by the way, actually my my bad is
09:32because our HTML doesn't refresh our
09:34JavaScript is refreshing.
09:36So in this case I just want to refresh
09:37'cause we changed our HTML again.
09:39But beyond that, let's go ahead
09:41and air style the style property.
09:42Okay, first of all, we have to change a few things.
09:44Fair enough, it's almost perfect, it's almost perfectly,
09:48we could just transcribe things the way they are,
09:50but there's a couple of small things we need to change.
09:52So first of all, let me just go ahead here
09:54and look at my word wrap to find it.
09:56'cause our styles in react are not described this way
10:00and they're actually described using objects.
10:02And to do that I open and close a curly bracket and I open
10:06and close a curly bracket.
10:08Again, this time around inside of here,
10:10I'm gonna just send an object notation
10:13and just literally send in the information.
10:15So in this case I'm gonna send here my background image
10:19and by the way, I could Otto uh, camel cases so I could go
10:22to my background image
10:25and just set it to be that value as a string.
10:28And I'm gonna go and set up Otto our color
10:32and I'm gonna set our color to be white
10:35and lemme just go ahead and delete that.
10:37So let's save this and see if that fixes things.
10:40And we're definitely getting closer.
10:41You can see our content is starting to get in
10:44and this is a great way to show you Otto, the process of
10:46how react actually works.
10:48Kind of like going backwards.
10:49So first of all, I could see that immediately.
10:51I'm getting here two errors.
10:53One is for that class, which I already hinted,
10:55that class itself is a reserved keyword in ES six,
11:00although we avoided the situation
11:01where we're getting an actual error in the JavaScript,
11:04we're getting an error in the rendering,
11:06we're not getting a J error from the JavaScript
11:08itself to sort that out.
11:09All I have to do is replace any place
11:11where there's an instance of class that is meant
11:14for a class to class name.
11:16Now then JSX will understand
11:18or more accurately, JSX is not gonna throw an error
11:21but React itself will know what I mean,
11:24which is basically a class name.
11:26And the second error that we were getting there was
11:29that we were using a property called Target.
11:32A Target doesn't exist in HTML
11:34and thus what it's basically telling us
11:36that we have an error in our HTML, which was much harder
11:39to get in any other way.
11:40So Target we go and now we fix that error as well.
11:43And if we go back in there, everything should be working
11:46and we shouldn't be getting any error.
11:49So in this lecture what we've done is we imported React
11:52And React DOM into our development
11:56or not into our development environment as we've done
11:58before, but as a dependency for our actual application.
12:02We're running on React version 15.2,
12:04but as we said, it really doesn't matter
12:06what is the version in the dot
12:08after the 15, as long as you're in version 15.
12:11If you're in a version that is earlier
12:13or later, then make sure to use the version 15 to be able
12:16to work with us in this lecture.
12:19Beyond that, we went ahead
12:20and we added Rea a React component
12:23and met JSX, which we saw JSX is basically HTML,
12:27although it's much more than that.
12:28We'll we'll explore a lot more of it throughout this title.
12:32And in this JSX we said that our diviv has to be,
12:36our element has to be a root element, which means
12:39that we can't have your two items,
12:41but it's one item together.
12:42This item starts and ends
12:44and all the content within is fine,
12:45but it has to have only one root element.
12:48We also said that we can't really use class
12:50because class is a reserved word in JavaScript,
12:53but with the help of Babel, that configuration
12:57that we added in for our Babel modules in our packages
13:00that we added in our React,
13:02we're not getting get any error from the htm,
13:04from the JavaScript itself,
13:06although we'll still get an error from React
13:08because React itself expects class name and not class.
13:11So we made sure that we've put that in.
13:13Last but not least, because we had your style, we had
13:16to make sure that we used the JSX style
13:19and to do the JSX style, we had
13:21to basically wrap it up in a object twice.
13:24And we're gonna take a deeper look into this throughout the
13:27title as well instead of typing that property directly.
13:31So that's it for this section in this lecture
13:35that wraps up this section.
13:37We finally created our first React component,
13:41but I'm sure you're aware that React goes way further
13:44and we're gonna explore so much more in depth react in the
13:47next section as we create E ES six React components.

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!