Components & Functionality (day 4)

Components & Functionality (day 4)

In this article:

  1. How React application work?
  2. How to create our own component and render it in the browser?
  3. Multiple components
  4. Practice yourself

How React application work?

  • React applications work by breaking the user interface into components. These reusable components can be composed to build complex user interfaces. Each component has its own logic and state that determines how it is displayed and what actions it can perform.
  • In the previous article, we discussed the react file structure, In the file structure there is one index.html file created by default by the vite.

index.html

<!DOCTYPE html
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + React</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.jsx"></script>
  </body>
</html>>        

  • In a React application, the index.html file is the entry point to the application and it contains the root div element with an id attribute of "root" where all the React components will be rendered.
  • The main.JSX file is the main file where we write our React components and it is the entry point for our JavaScript code.
  • To connect the two files, we need to include the main.JSX file in the index.html file using a script tag with a type attribute of "module" to indicate that it is a JavaScript module.

In the main.JSX file, we need to import the React and ReactDOM libraries and create our React component(s) using JSX syntax. We then use the ReactDOM.render() method to render our component(s) inside the root div element in the index.html


import React from 'react'
import ReactDOM from 'react-dom';

function App() {
  return (
    <div>
      <h1>Hello, World!</h1>
      <p>This is my first React app.</p>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));;        

  • In this example, we create an App functional component that returns a div element containing an h1 and a p element. We then use the ReactDOM.render() method to render the App component inside the root element in the index.html file.
  • When the React app is run, the index.html file is loaded in the browser, which in turn loads the main.JSX file renders the App component inside the root element, resulting in the output of the h1 and p elements.

How to create our own components?

Creating our own components is very easy, This process is like creating folders and files on our local pc and laptop. Let see how

No alt text provided for this image
File structure

  • Your right-side image shows how the react application files are structured.
  • If you want to create your own components you just go to the "src" folder and right click there is a new file option available to click that option the VS code creates a new file for us. Rename the file name whatever you want but after the filename, you need to put any of the .js, .jsx, .ts, .tsx extensions.
  • Then go to the App. JSX file and clear the jsx elements inside the return method. Because the JSX elements are the default structure, you do not need to keep that structure in your project.

No alt text provided for this image
App.jsx

  • After you clear all that stuff then it will look like this. You can also delete the reactLogo and viteLogo if we no longer need them.
  • And also delete the useState we discuss in the later articles.
  • After clearing all the stuff keep that file open.



Multiple Components:

Inside the src folder, I created two files namely NavBar. jsx and BodyPart. jsx. These files are two separate components. What we are going to do is, we are rendering these two components on the browser. Let's see what to do.

No alt text provided for this image
NavBar. jsx

I created a functional component inside the Navbar. jsx file.

Inside the NavBar functional component, I return the <h1> element.

Finally, I export the NavBar function component.

Then the BodyPart. jsx I created a <p> element.


No alt text provided for this image
BodyPart. jsx

Now we are going to import these two components in our App. jsx component. Let's see,

No alt text provided for this image
App. jsx

From the top, I imported the two files the NavBar and BodyPart files using the import module.

Inside the App functional component, we are using the NavBar and BodyPart components. This is the way to add other components to our App component.

Practice Yourself:

  1. Create a new React application using CRA or Vite, Then run the application on the browser. Then clear all the content inside the return method from the App. jsx file.
  2. Create two or more files inside the src folder, Then import the files inside your App. jsx file.
  3. Then go to the CSS files to change the styles because next chapter I will discuss styles.

That's it KIDS🤘see you in the next article.

To view or add a comment, sign in

More articles by Kalyanasundar Arunachalam

  • Push() Method

    The JavaScript method is one of the most commonly used functions to append elements to the end of an array. However…

  • 🔁Recursion

    Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem until…

  • Merge Sort

    Sorting is a fundamental concept in computer science. Whether you're working on a large dataset or a small array…

  • Array

    Understanding Arrays in JavaScript: Arrays are fundamental data structures in programming, and in JavaScript, they…

  • Types of Data Structures

    In the ever-evolving field of software development, mastering Data Structures and Algorithms (DSA) is crucial. Data…

  • Space complexity

    When learning about data structures and algorithms (DSA), one crucial concept to grasp is space complexity. Just like…

  • ⏲️Time complexity

    Understanding Time Complexity in Algorithms Time complexity is a critical concept in computer science that measures the…

  • DSA introduction

    What is DSA? Data Structures and Algorithms (DSA) is a fundamental concept in computer science that involves the study…

  • Intro to Blockchain

    Before we delve into what Blockchain is, it's important to understand how the web has evolved. Don't worry; I'll…

  • NodeJS

    Hey fellow dev's! 👩💻👨💻 It's buzzing out there about MERN stack and Node.js, but let's hit the brakes and dive into…

Others also viewed

Explore content categories