Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a component in ReactJS

Introduction to ReactJS

ReactJS, often referred to as React, is a popular JavaScript library used to build user interfaces, particularly single-page applications. Think of it like a tool in a developer's toolbox that helps you code more efficiently. It's as if you're a carpenter and React is your power drill, it gets the job done quickly and efficiently.

What is a Component?

A component in React is like a building block in a LEGO set. Each block can stand alone or be combined with others to create a larger structure. In terms of programming, a component is a reusable piece of code that controls a part of the user interface. It's like a blueprint for a part of your web page.

Adding a Component in React

To add a component in React, you first need to understand some basic concepts, such as JSX and props. Don't worry, we will walk through each step in detail.

JSX

JSX is a syntax extension for JavaScript that looks similar to HTML. It allows you to write HTML-like code in your JavaScript file. Here's a simple example:

let element = <h1>Hello, world!</h1>;

In the above example, <h1>Hello, world!</h1> is a JSX tag. It's not a string nor HTML, but it will become real HTML once it's rendered on the web page.

Components and Props

Components let you split the UI into independent, reusable pieces. Props, short for properties, allow components to talk to each other. If components were people, props would be their language.

We can create a component in two ways - as a function or a class. For this guide, we will use function components as they are simpler to understand and use.

Here's how you can define a component:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

In the above code, Welcome is a function component that accepts props as an argument and returns a JSX element. The JSX is using curly braces {} to embed JavaScript expressions - in this case, a prop named name.

Rendering a Component

Rendering a component means telling React what you want to see on the screen when the component is used. You do this with the ReactDOM.render() method. It's like a painter applying brush strokes to a canvas to create an image.

Here's how you can render the Welcome component:

ReactDOM.render(
  <Welcome name="Sara" />,
  document.getElementById('root')
);

In the code above, <Welcome name="Sara" /> is the component we defined earlier. We're telling React to render this component and put it inside a DOM element with the id root. The result on the web page will be Hello, Sara.

Building a Complex Component

Now that we know how to create a basic component, let's build something more complex. Imagine we're creating a social media profile card. This card would have a user's picture, name, and a button to follow the user.

First, we create the ProfileCard component:

function ProfileCard(props) {
  return (
    <div>
      <img src={props.image} />
      <h2>{props.name}</h2>
      <button>Follow</button>
    </div>
  );
}

In the component above, we're returning a <div> with an <img>, a <h2>, and a <button>. The image source and the name are passed as props.

Now, let's render this component:

ReactDOM.render(
  <ProfileCard name="John Doe" image="john.jpg" />,
  document.getElementById('root')
);

With the code above, we're rendering a ProfileCard with the name John Doe and the image john.jpg.

Conclusion

Adding a component in React might seem a little intimidating at first, but with practice, it becomes second nature. Remember, a component is just a reusable piece of code that controls a part of the user interface. It's like a LEGO block that you can use to build your web application.

So, keep practicing, keep building, and soon you'll be creating complex applications with ease. Happy coding!