Making Components

React projects are built with Components. To build web applications with React you need to think in Components.

React would best be described as a library for creating user interfaces. Think of Components as the elements of user interfaces. For example, your site might be made up of the following Components:

App, Header, Logo, NavBar, NavLink, Content, Card, Footer

Follow the steps below

Step 1:

Run the below mentioned command in your npm.

  • npx create-react-app my-appĀ (You cab name the directory anything you want. I am going to name it my-app)
  • Yarn start(run this command next)
  • Every time your working with react make sure to run yarn start in your terminal.
  • Now go to you visual code and open the file that you created( Which is my-app)

Click on Open folder and select the folder you have created.

It should look something like this

If you want to push the codes on GitHub. Please go ahead and create a Repository and kindly push the codes to save your progress.

Step 2:

  • Create a file App.js inside of your src folder (where you can import you files and body content)
  • Create a file index.js inside of src folder (where you import your React and ReactDom files along with index.css
  • Now create a folder Components inside of your src folder.
  • Inside of components create a folder Layout
  • Inside of Layout create a folder Header
  • Inside of Header create a file index.js and style.css

Step 3:

Create a function inside of index.js file in Header folder. I am going to name it Header. Do remember that when you create a function in React. The name starts with a capital letter.

import './style.css';

  function Header () {
    return (

      // the html content here 

    )
  }

export default Header;

Once i add the html and style it looks like this.

import './style.css';

function Header() {
    return (
      <nav className ="navbar navbar-expand-lg navbar-light bg-light">
        <div className="container-fluid">
          <a className="navbar-brand" href="#">Navbar</a>
          <button className="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
    </button>
            <div className="collapse navbar-collapse" id="navbarSupportedContent">
              <ul className="navbar-nav me-auto mb-2 mb-lg-0">
                <li className="nav-item">
                  <a className="nav-link active" aria-current="page" href="#">Home</a>
                </li>
                <li className="nav-item">
                  <a className="nav-link" href="#">Link</a>
                </li>
                <li className="nav-item dropdown">
                  <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a>
                  <ul className="dropdown-menu" aria-labelledby="navbarDropdown">
                    <li><a className="dropdown-item" href="#">Action</a></li>
                    <li><a className="dropdown-item" href="#">Another action</a></li>
                    <li><hr className="dropdown-divider"/></li>
                    <li><a className="dropdown-item" href="#">Something else here</a></li>
                  </ul>
                </li>
                <li className="nav-item">
                  <a className="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
                </li>
              </ul>
            <form className="d-flex">
              <input className="form-control me-2" type="search" placeholder="Search" aria-label="Search"/>
              <button className="btn btn-outline-success" type="submit">Search</button>
            </form>
          </div>
        </div>
      </nav>
    )
}

export default Header;

Make sure to import the header folder inside of your App.js file

import Header from './components/layout/header';

Your yarn.lock file looks something like this. Make sure to not change anything in this particular area.

Your public folder should have a index.html folder and the needed content plus links for the style to work

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>
</html>

You can use you own idea and creations. Go ahead and try for yourself.