Sunday, September 11, 2022

Learn React 18: Introducing Components

Learn React 18: Introducing Components

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 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 lesson. 

Creating a React Component

Lets 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 function will return a React element. Our helloElement from the previous tutorial looked like this:

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

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.

Lets modify it so that is accepts a prop object as an argument with all the information that it needs.

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.

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.

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 for creating 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.


No comments:

Post a Comment