React: Behind the Scenes Part 2
Tags: webdev, javascript, beginners, react
Published: 2023-10-25 19:55:08

In the [previous article](https://dev.to/thecoollearner/react-behind-the-scenes-part-1-36lh), we explored the process of creating a "Hello World!" React app using just a single HTML page. In this article, we will delve into the separation of our JavaScript code from our HTML page and learn how to create reusable components.

---

This is the code we have till now :-

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React BTS</title>
  </head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.2.0/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
    <script>
      const App = () => {
        return React.createElement(
          "div",
          {},
          React.createElement("h1", {}, "Hello World!")
        );
      };

      const container = document.getElementById("root");
      const root = ReactDOM.createRoot(container);
      root.render(React.createElement(App));
    </script>
  </body>
</html>
```

Now, let's first move our JS code to a separate file called **app.js** and include it into our HTML page using a script tag -

```html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React BTS</title>
  </head>
  <body>
    <div id="root"></div>

    <script src="https://unpkg.com/react@18.2.0/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
    <script src="./app.js"></script>
  </body>
</html>
```

```javascript
// app.js
const App = () => {
  return React.createElement(
    "div",
    {},
    React.createElement("h1", {}, "Hello World!")
  );
};

const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
root.render(React.createElement(App));

```

If we run this project now, the output will remain the same.

---

Let's create our first component -

```javascript
// app.js

const Student = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "John"),
    React.createElement("h2", {}, "Male"),
    React.createElement("h3", {}, "21"),
  ]);
};

const App = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "Hello World!"),
    React.createElement(Student),
  ]);
};

const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
root.render(React.createElement(App));
```

Here we have created a component called **Student** which uses **React.createElement** to create a DOM structure with a parent "div" element and three child elements ("h1," "h2," and "h3"). When this component is rendered, it will produce the following HTML structure:

```html
<div>
  <h1>John</h1>
  <h2>Male</h2>
  <h3>21</h3>
</div>
```

And the output would be something like :


![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2416i5lzdveh5ddhg0j.png)

Now that we have a component, we can utilize it multiple times by simply calling it. :

```javascript
// app.js

const Student = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "John"),
    React.createElement("h2", {}, "Male"),
    React.createElement("h3", {}, "21"),
  ]);
};

const App = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "Hello World!"),
    React.createElement(Student),
    React.createElement(Student),
  ]);
};

const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
root.render(React.createElement(App));
```

And the output would be : 


![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/533lmopl4y58q40zt18y.png)

Here, we can see that it doesn't make any sense to use the component twice, as it gives the same output. That's because, until now, we have been using static data only. Let's make this component dynamic using props:

```javascript
// app.js

const Student = (props) => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, props.name),
    React.createElement("h2", {}, props.gender),
    React.createElement("h3", {}, props.age),
  ]);
};

const App = () => {
  return React.createElement("div", {}, [
    React.createElement("h1", {}, "Hello World!"),
    React.createElement(Student, { name: "John", gender: "Male", age: 21 }),
    React.createElement(Student, { name: "Mary", gender: "Female", age: 20 }),
  ]);
};

const container = document.getElementById("root");
const root = ReactDOM.createRoot(container);
root.render(React.createElement(App));
```

Here, we have used props to pass some dynamic data to the component, and then we are using them accordingly. Let's see the output:


![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i95w839hngwq09zbk3a.png)

---

As a result, we've successfully separated our JavaScript code from the HTML, created components, and gained a deeper understanding of how React operates.