Create a Simple ReactJs Application – Part 2

by Aug 4, 2022React JS

In the tutorial’s last part, we discussed about ReactJs and how to run a simple react app on a web browser. In this part, we will dig more into some interesting stuff, such as creating a home, about us, and contact pages.

Click here for the first part – Create a Simple ReactJs Application – Part 1

Hello World!!

If you have only one page to view in the application, then that’s not a big task, simply changing the App.js does wonders.

We will print a simple “Hello World!!” on our application.

Navigate to src/App.js in your project directory. Open the file with your favorite editor. You will find a code like this which is shown below;

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App; 

Now, delete the unwanted lines from the code and add the necessary part.

import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
	<h1>Hello World!!</h1>
</div>
);
}
}
export default App;

Save this file in your directory, and run the command npm start

If you have to view two or more webpages, we will be using React Router to navigating to the various components.

We will use React Router to navigate various components in the react application. It allows changing the browser URLs and keeping in sync with the URL. We will create three components: a home component, an about us component, and a contact component, i.e., each component for each page.

Installing React Router

Navigate to your project location, run the following command to install the React Router. It is installed via the npm.

npm install react-router-dom

After the successful installation of the react-router-dom package, you have to create four different files in your components folder in the src folder. Follow the folder structure as below,

|src
  |--components
     |--index.js
     |--home.js
     |--contactus.js
     |--aboutus.js
 

Copy the following code in your respective files:

  • home.js
import React from 'react';
const Home = () => {
    return (
        <div>
            <h1>Welcome to IoTEDU</h1>
            <p>Home Page</p>
        </div>
    );
};
export default Home;
  • contactus.js
import React from 'react';

function Contact() {
    return <address>
        You can write to:<br />
        info@iot4beginners.com<br />
    </address>
}

export default Contact;
  • aboutus.js
import React from 'react';
const Aboutus = () => {
    return (
        <div>
            <h1>About us</h1>
            <p>We are committed to writing blogs and tutorials on IoT, from basic to advanced topics to make the learners understand easily. IoTEDU is considered a one-stop for blogs, tutorials, projects, the latest software, and hardware update for the learners to motivate them to learn more and more to enrich their knowledge. Started in 2019, we proudly say that we achieved a place in the IoT learners community.</p>
        </div>
    );
};
export default Aboutus;
  • index.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';
import Home from './home';
import Aboutus from './aboutus';
import Contactus from './contactus';


class App extends Component {
    render() {
        return (
            <Router>
                <div className="App">
                    <ul className="App-header">
                        <li>
                            <Link to="/">Home</Link>
                        </li>
                        <li>
                            <Link to="/aboutus">About Us</Link>
                        </li>
                        <li>
                            <Link to="/contactus">Contact Us</Link>
                        </li>
                    </ul>
                    <Routes>
                        <Route exact path='/' element={< Home />}></Route>
                        <Route exact path='/aboutus' element={< Aboutus/>}></Route>
                        <Route exact path='/contactus' element={< Contactus />}></Route>
                    </Routes>
                </div>
            </Router>
        );
    }
}

export default App;

After creating these files, modify the App.js file

import Webpages from './components';
function App() {
return (

);
}
export default App;

Run your Application

npm start

You can view your routed links and pages in your application on the browser (localhost).

Home page

homepage

About Us Page

aboutus

Contact Page

contactus

You can also add some CSS and add in the app.css file. Finally, you can also build the project and host it on any server. We will discuss about the hosting in the next part of our tutorial, Happy learning!

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Create a Simple ReactJs Application – Part 1

Create a Simple ReactJs Application – Part 1

ReactJs is one of the famous front-end open-source libraries of JavaScript developed by Facebook. It aims to allow developers to quickly create fast user interfaces for websites and applications. In this blog, we will create a simple and basic ReactJs...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....