1. Code
  2. JavaScript
  3. React

Learn React 18: Introducing Components

Scroll to top
60+ min read

Components are the building blocks of any React app. If you want to learn React, you have to start with components!

In this lesson from our free full-length beginner course on React 18, you'll learn how. 

Components are the building blocks of a React app. They are somewhat similar to a JavaScript function that you might define in a program. They are independent entities that make up the UI of your app, and you can reuse them again and again.

In fact, you can define a component using classes or functions. Functional components are the most up-to-date technique, so that's what I'll show you in this tutorial. 

Creating a React Component

Let's begin by updating our "Hello World" app from the first tutorial so that it uses a React component. We will modify it in a manner that allows us to say hello to someone specific instead of the whole world.

When React components are defined as functions, you can pass them a single object argument called props with all the required data. These functions will return a React element. Our helloElement from the previous tutorial looked like this:

1
let helloElement = React.createElement('h1', {id: 'greeting', className: 'hello'}, 'Hello, World!');

We can convert it to a function that returns a React element by using the code below:

1
function Greeting() {
2
    return React.createElement('h1', {id: 'greeting', className: 'hello'}, 'Hello, World!');
3
}
4
5
let helloElement = React.createElement(Greeting);

Opening the webpage in the browser will still show us the same old Hello World! message because the message is hard-coded into the function.

Let's modify it so that it accepts a prop object as an argument with all the information that it needs.

1
function Greeting(props) {
2
    return React.createElement('h1', {id: 'greeting', className: 'hello'}, `Hello, ${props.name}!`);
3
}
4
5
let helloElement = React.createElement(Greeting, {name: 'Nitish'});

There is another way of creating our component, as shown below. However, keep in mind that a simple function call like this will only work if the Greeting component does not have any hooks (it doesn't in this case). I would suggest that you stick with the above method of creating components.

1
function Greeting(props) {
2
    return React.createElement('h1', {id: 'greeting', className: 'hello'}, `Hello, ${props.name}!`);
3
}
4
5
let helloElement = Greeting({name: 'Nitish'});

Open the webpage in a browser, and you will see "Hello, Nitish!" instead of "Hello, World!". We can say hello to anyone now.

React Components: Hello GreetingReact Components: Hello GreetingReact Components: Hello Greeting

Creating More Complicated Components

The code we have written so far to create components is manageable. However, we have only defined a very basic component consisting of a single heading element. In real life, you will have to create much more complicated layouts, and using the above method for creating components will no longer be feasible.

The following example creates another component that will demonstrate how cumbersome it can get to create complicated components with multiple children.

1
function Country(props) {
2
    return React.createElement('div', {
3
        className: "container"
4
    }, React.createElement('h2', {
5
        className: "country-name"
6
    }, `Country Name: ${props.name}`), React.createElement('p', {
7
        className: "capital"
8
    }, `Capital: ${props.capital}`), React.createElement('p', {
9
        className: "population"
10
    }, `Population: ${props.population}`));
11
}
12
13
let countryElement = React.createElement(Country, {name: 'United States', capital: 'Washington, D.C.', population: '332 million'});
14
15
let rootElement = document.getElementById('root');
16
ReactDOM.createRoot(rootElement).render(countryElement);

Once you refresh the webpage, the markup in the inspector tab will look like the image below:

React Country Component MarkupReact Country Component MarkupReact Country Component Markup

I used a little CSS to make the component more presentable.

React Country Component RenderReact Country Component RenderReact Country Component Render

You can see the final result of executing the above code in a browser in the CodePen below. Try modifying the code to add one more element to the component which shows the area of the United States.

Final Thoughts

We were able to output our component the way we wanted. However, you can see that it required us to write a lot of code. Writing so much code to create simple components is error-prone and will bring down productivity.

There are a bunch of tools and libraries out there that can help you create a React app by writing much less code. The code for our component could also be shortened significantly by using JSX. We will learn about that in the next tutorial.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.