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

Creating a React Component

Creating a React Component: A Quick Recap for Beginners

In this lecture, we explore how to create a React component using JSX and ES6. While this course assumes prior knowledge of React, this quick recap aims to refresh your memory and help reinforce fundamental skills necessary for building efficient React components. Let's dive into the process and review some key concepts that will be useful as you continue on your React journey.

React Components: The Building Blocks of UI

A React component is a small, reusable piece of UI that can be used to build more complex interfaces. Components can be written either as functional or class-based components, but this lecture focuses on class components. The process begins with setting up a file structure to organize your components effectively.

Setting Up Your Component Directory

Organizing components properly is essential for scaling applications. In this lecture, we start by creating a new folder called components within the src directory of our project. This folder will hold all the individual components that make up our application. We then create a new file named Counter.js, which will house our counter component.

Creating a Simple Counter Component

React components are JavaScript classes or functions that extend React.Component (for class-based components). Below is the basic structure of a counter component in ES6:

import React, { Component } from 'react';

class Counter extends Component {
  render() {
    return (
      <div>
        <h2>0</h2>
        <button>+</button>
        <button>-</button>
      </div>
    );
  }
}

export default Counter;

Key Steps in Creating the Counter Component

  1. Import React: We begin by importing React from the React library. This import is essential since JSX is syntactic sugar that translates to React function calls.
  2. Define the Component Class: The Counter class extends React.Component, giving it access to various lifecycle methods and state management tools.
  3. Render Method: The render() method is a key part of any class-based component. It determines what gets displayed to the DOM. In our counter component, we use a <div> element containing an <h2> for displaying the current count, and two buttons (+ and -) for incrementing and decrementing the counter.

Understanding JSX in Components

JSX is an extension of JavaScript that allows us to write HTML-like syntax directly within JavaScript. It makes building React components more intuitive. The render() method in our Counter component returns JSX that represents our UI. JSX will be compiled into standard JavaScript by Babel, allowing it to be read by browsers.

In the example above, the JSX is used to create an HTML structure with an <h2> tag and two buttons. These elements will be rendered in the browser when the component is loaded. Notice how we use braces {} to include expressions within JSX, such as dynamic values or JavaScript logic.

ES6 Features in React Components

React encourages the use of modern JavaScript features, many of which come from ES6 (also known as ECMAScript 2015). Some ES6 features used in this example include:

  • Classes: React components are often written as JavaScript classes. This allows us to define reusable blueprints that can maintain their internal state and have access to lifecycle methods.
  • Module Imports/Exports: The import and export statements allow us to split our code into separate files, which can then be reused across different parts of our application. This improves modularity and makes our codebase easier to maintain.

Common Errors and Debugging Tips

While creating React components, developers often encounter some common issues, particularly when dealing with exports and JSX syntax. One example covered in the lecture involves forgetting to export the component, resulting in errors when trying to use the component elsewhere. Always ensure your component is exported properly, either using export default or named exports.

Additionally, React throws warnings for invalid nesting in JSX. For instance, a <div> cannot be a child of a <p> (paragraph) tag. This lecture highlights these types of warnings, emphasizing the importance of properly structuring HTML elements within JSX to avoid validation errors.

Adding the Counter to Our App

To add the Counter component to our main application, we import it into App.js:

import Counter from './components/Counter';

We then include the <Counter /> tag within the render() method of our App component. This allows us to use the counter in our main application interface.

Enhancing Development with React DevTools

To make the development process smoother, this lecture also recommends installing React Developer Tools, a browser extension that allows developers to inspect and debug React components directly within their browser’s developer console. This tool is particularly useful when working with more complex React applications, as it allows you to see the component hierarchy, props, and state at a glance.

React DevTools is available for both Chrome and Firefox, though the lecture suggests using Chrome, as it tends to be better supported for React development.

Conclusion

In this lecture, we revisited how to create a basic React component using JSX and ES6. We covered the steps involved in creating a simple counter component, explored how to properly import and use components, and discussed common errors to avoid while building components. These are foundational skills that will be crucial as we progress in building more dynamic applications.

In the next lecture, we'll explore how to manage state within React components, providing more dynamic and interactive features. We'll also learn about using external data sources to populate our components, making our applications more connected and useful.

Stay tuned, and happy coding!

Video Transcript

00:01In the last lecture, we set up our environment
00:03and in this one we're gonna start creating
00:05our React component.
00:07In the process, we're gonna have a refresh on ES six
00:10or maybe catch you up onto the latest ways
00:12to work in JavaScript and ES six
00:14and also a refresh on JSX, which is one
00:17of the most common ways to build React component.
00:20With that said, let's jump right into it
00:22and let's build our React component.
00:32Alright, let's continue.
00:34Now I'm gonna go ahead
00:35and create a very basic component in React.
00:38Now we're not gonna cover everything about React,
00:40and you could follow many of my other titles about React,
00:44which you could find in the library
00:45that you're currently at.
00:47Just search for Ben React
00:49and you'll find quite a lot of different react titles.
00:52With that said, let's go ahead
00:54and continue down our process of building.
00:56And you know what, just for the sake of, um, visualization,
00:59I'm just gonna change it to 1.2
01:00'cause we're in chapter one, lecture two currently.
01:04Now I'm gonna go ahead
01:05and what I'm gonna do is I'm gonna go into my source folder
01:08and create a new folder.
01:10I'm gonna call that folder components.
01:13In our components directory.
01:15I'm gonna literally put all of my components.
01:17Now in this case, what I wanna do is I want
01:20to create a counter to do that, I'm gonna go ahead
01:23and I'm gonna create a new file
01:24and I'm gonna call this counter
01:26and it's gonna be a a JS file.
01:29And literally, I'm just gonna go ahead
01:30and save that file into that directory of components.
01:34I'm gonna go into my app directory
01:36and let me just go ahead here
01:37and I'm just gonna copy everything inside of our app,
01:40go into our counter and paste everything inside there.
01:44Now I'm just gonna change a few things.
01:46For one, I'm gonna create a counter
01:49and we're also gonna export a counter.
01:52Now, you know what, just for the sake of our, uh,
01:55showing you all the steps involved, let me just go ahead
01:57and delete this as we're in the process of showing you
02:01how it's done.
02:02So, the first step that we're gonna do,
02:04and let me just go ahead and delete everything,
02:05literally everything, the first step is I'm gonna go ahead
02:08and import React.
02:10I'm gonna import it from the library, react.
02:12Now notice that I'm, it's,
02:14I'm literally just putting the full qualified name
02:17and that's what I do whenever I'm loading in a library
02:20from NPM itself or literally from the node modules.
02:23So if I go into my package, you'll see that one
02:26of the packages is React
02:28and that is the one that I'm loading up.
02:31So now that I have React, the React itself, load it up,
02:35I could and I could go ahead and use React if I need to.
02:39In this case, I'm not really using it,
02:41but what I really need to,
02:42to access right now is the component itself.
02:45And by the way, I do use React
02:47'cause React is in the JSX here,
02:49this JavaScript is automatically using its, so we need
02:52to have access to the React itself.
02:55The next thing that I'm gonna load up is the
02:57component component.
03:01Now the component itself is the, you could say the starter
03:05for any single component in React.
03:07And literally the script is, as you can see here,
03:10you name the name of the component
03:12and then extend that component with the component.
03:15So let's go ahead and do that. So I'm gonna go ahead
03:17and create a new class.
03:19I'm gonna give it a name, which in our case we decided it
03:21will be counter and I'm gonna get it from, well, not from,
03:25yeah, no, no, no.
03:27What what, what, what am I doing? Counter.
03:30And we're gonna extend our component.
03:34And every single component has a, has a render method.
03:39The render method itself is literally in charge of
03:43bringing back our value.
03:45And in this case, I'm gonna set you a return.
03:47I'm gonna open up a round bracket.
03:49And by the way, I'm doing that just so I could break lines
03:51because I want to make sure that it's indented
03:54and easy to read.
03:55If not, I could have started here,
03:56but then I would have to close my diviv down below here.
03:59So this is a nice way to make sure
04:01that you could spread your code on a few more, more lines
04:04so it's a little bit cleaner,
04:05a little bit easier for you to render.
04:07Alright, so in this case, I'm gonna go ahead
04:09and create a diviv and literally here I'm just gonna put
04:12simple HTML,
04:13which will automatically be transcribed down into React
04:17elements using the React itself.
04:19That's why you wanna make sure
04:20that we've imported React itself.
04:22The next thing I'm gonna do is I'm just gonna go ahead
04:25and I'm gonna set here an H two.
04:28And literally this is just simple HTML.
04:30And let me just start it by putting here the value of zero.
04:34And I'm gonna also create your A button.
04:38The first button will be a plus button.
04:40The second button will be a minus button.
04:43So if I go ahead here I have your A
04:44plus, I have your A minus.
04:47Beautiful. Alright, so there we go.
04:49That is our, our component.
04:51In a nutshell, all that's left for us to do now
04:54that I've saved it, is
04:55to go back into our application itself and load it up.
04:58So I'm gonna go and I'm gonna go ahead and import it.
05:00So I'm gonna go ahead
05:02and import our counter from
05:08component opponents counter.
05:11Notice how I'm not using js,
05:13but this time around I'm using slash to make sure
05:16that I know this is not one of the node modules,
05:19but I'm actually trying
05:20to fetch this from a local directory.
05:22So I got here at my counter now that I have my counter.
05:25And also it's very important to note
05:26that we're also starting it with an uppercase letter.
05:31I'm gonna go ahead here and just delete
05:32everything inside of here.
05:33And I'm literally just gonna assign in our counter
05:36with JSX making sure I'm using an uppercase
05:39'cause that's gonna tell, uh, the JSX render
05:42or when React is rendering.
05:44When we're compiling this down, this is gonna tell
05:46that this is an external file and not simple HTML.
05:50Anything that starts with an upper case literally tells the
05:53renderer with JSX that this is expected to be actually,
05:56actually a real component and it's gonna get it from there
05:59versus if it was just a simple diviv or image.
06:02Now if I go into my browser, you'll see that first of all,
06:04I'm getting a few errors here.
06:06So first of all, warning, react, create element is invalid.
06:09Expecting a string for built in components
06:12or class functions for composite for,
06:14but we got an object likely you forgot
06:17to export your component from the file.
06:19It's defined in beautiful. So it's incredibly detailed.
06:22This error. What it's actually telling us in more plain
06:25English is that our counter itself is not actually exported
06:30and it's telling us that actually counter
06:31doesn't have anything inside of it.
06:33So I'm gonna go ahead here
06:34and I'm gonna make sure that I export default counter,
06:39literally telling our application that
06:42whenever we load up the counter
06:43by default it's gonna fetch our counter on contrary
06:47by the way, when we took a component that is something
06:50that is a component that is not a default but is exported.
06:53And we're gonna see more of that
06:55as we progress in the title itself.
06:57But the most important thing right now is
06:59that we fetched our counter
07:01and if we go back into our browser again,
07:03we'll get a lot less errors.
07:04First of all, we're gonna get our component.
07:06Secondly, we could see that this is a warning,
07:08not exactly an error,
07:09but a warning that says
07:11that when it's validating our nesting, a div cannot appear
07:15as a descendant of a paragraph.
07:17So in that case, we want to go inside of our component
07:20and make sure that our counter is not a
07:22child of a paragraph.
07:24So let me just check our app intro here,
07:26which was our starter CSS file.
07:29And our app intro does not care if it's a paragraph.
07:31We're not beautiful. So I'm just gonna go ahead here
07:34and I'm gonna set it to be a diviv instead of a paragraph
07:36because we don't need it to be a paragraph and anymore.
07:39And I'm gonna go ahead and save our file.
07:42And if I go back into my browser
07:43and we should now get our application running,
07:45we without an actual error going on now, by the way,
07:49before we continue with PI, would be a good idea for us
07:52to have Oso the React dev tools installed
07:55so we could have a better development experience.
07:57So just go ahead in the console if you don't have it already
08:01installed and just go ahead into the tool itself
08:03and find the install button, which will be, gosh, who,
08:08where is it
08:15Installation?
08:17Boom, we're in fire, we're in Chrome.
08:19In your case you might be in fire effects.
08:20By the way, you're better off to use Chrome
08:22because a lot of the developers of React
08:24and Oso of Redux such
08:28are running on, on, on Chrome.
08:30So you have a better probability
08:32that the developer tools there will be more up to date
08:34or better working and we'll add that extension
08:37and in the next lecture after I, um, yeah, that's it.
08:41So the next lecture, if I go ahead here
08:43and o to just check to see if our tool,
08:46let me just click here and refresh to see.
08:47There we go. We're not getting that error anymore
08:50and we are
08:53good in this lecture.
08:56We created a React component using ES six and Oso JS six.
09:00In the next lecture we'll continue down the path
09:03as we'll show you the basics of how
09:05to work with React states.
09:06Before we move on to using external data sources, I.

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!