COVID-19 Tracker using ReactJS and real time API
Last Updated :
25 Jul, 2024
Creating a web application COVID-19 Tracker using ReactJS and real-time API. In this web application or website, when the user enters the name of the country, it will display the number of active cases, recovered cases, today's cases, etc.
Prerequisites:
Approach:
- Set up the development environment, and install all the required dependencies.
- In the App.js file, create and initialize a component that is used to hold the code of the web application.
- In that JavaScript file, create a form to take the input from the user and fetch and display the details using real-time API and react useState Hook and useEffect Hook.
- Use CSS for stylings of the component file and import the CSS file in the component file.
Steps to Create React Application:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
In that created folder go to src folder and delete App.test.js, logo.svg and setupTests.js because these files are not required in this project, and add component files used to hold the code for the application. Our component name is CovidData and file name is CovidData.js for stylings add the CSS file CovidData.css.
Project Structure:
Project StructureExample: This example uses fetch funtion to get the data form API and CSS for styling.
CSS
/* Filaname - CovidData.css */
body {
background-color: rgb(102, 226, 102);
}
.covidData {
background-color: green;
width: 30%;
margin: auto;
margin-top: 15px;
border-radius: 6px;
padding: 10px;
}
/* input stylings */
.covidData__form {
padding: 10px;
margin: 20px;
}
.covidData__input input {
width: 400px;
height: 50px;
text-align: center;
font-size: 25px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
}
.covidData__input button {
width: 80px;
height: 50px;
margin-top: 10px;
background-color: black;
color: white;
font-size: 20px;
border-radius: 10px;
border: none;
box-shadow: 5px 5px 5px rgb(71, 67, 67);
cursor: pointer;
}
/* detail stylings */
.covidData__country__info {
font-size: 20px;
font-family: Verdana, Geneva, Tahoma, sans-serif;
width: 500px;
margin-left: auto;
margin-right: auto;
height: auto;
padding-left: 10px;
background-color: white;
margin-top: 20px;
padding: 2px;
text-shadow: 1px 1px 1px rgb(71, 67, 67);
box-shadow: 5px 5px 5px rgb(71, 67, 67);
}
JavaScript
// Filename - App.js
import CovidData from "./CovidData";
function App() {
return (
<div className="App">
<CovidData />
</div>
);
}
export default App;
JavaScript
// Filaname - CovidData.js
import React, { useEffect, useState } from "react";
import "./CovidData.css";
function CovidData() {
const [country, setCountry] = useState("");
const [cases, setCases] = useState("");
const [recovered, setRecovered] = useState("");
const [deaths, setDeaths] = useState("");
const [todayCases, setTodayCases] = useState("");
const [deathCases, setDeathCases] = useState("");
const [recoveredCases, setRecoveredCases] =
useState("");
const [userInput, setUserInput] = useState("");
useEffect(() => {
fetch("https://disease.sh/v3/covid-19/countries")
.then((res) => res.json())
.then((data) => {
setData(data);
});
}, []);
const setData = ({
country,
cases,
deaths,
recovered,
todayCases,
todayDeaths,
todayRecovered,
}) => {
setCountry(country);
setCases(cases);
setRecovered(recovered);
setDeaths(deaths);
setTodayCases(todayCases);
setDeathCases(todayDeaths);
setRecoveredCases(todayRecovered);
};
const handleSearch = (e) => {
setUserInput(e.target.value);
};
const handleSubmit = (props) => {
props.preventDefault();
fetch(
`https://disease.sh/v3/covid-19/countries/${userInput}`
)
.then((res) => res.json())
.then((data) => {
setData(data);
});
};
return (
<div className="covidData">
<h1>COVID-19 CASES COUNTRY WISE</h1>
<div className="covidData__input">
<form onSubmit={handleSubmit}>
{/* input county name */}
<input
onChange={handleSearch}
placeholder="Enter Country Name"
/>
<br />
<button type="submit">Search</button>
</form>
</div>
{/* Showing the details of the country */}
<div className="covidData__country__info">
<p>Country Name : {country} </p>
<p>Cases : {cases}</p>
<p>Deaths : {deaths}</p>
<p>Recovered : {recovered}</p>
<p>Cases Today : {todayCases}</p>
<p>Deaths Today : {deathCases}</p>
<p>Recovered Today : {recoveredCases}</p>
</div>
</div>
);
}
export default CovidData;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
Create a COVID-19 Tracker CLI using Node.js In this article, we will see how to create a command-line Corona Virus Tracker using Node.js. We will track total cases, active cases, totally recovered cases, and total deaths in the Indian States. Approach: We use an npm package called 'request' to fetch data from publicly available covid-19 API
2 min read
Progress Tracker using React and Local Storage Progress Tracker using React and local storage is a basic website that lists the tasks that the user has to complete. When the user completes any one of the topics, he/she can tick mark the topic as completed and the progress will be shown to him/her. To store this progress, so that the user can com
6 min read
Movie Trailer app using ReactJS In this article, we are going to make a simple app that searches for any movie/web series trailers. The users can search for their favourite movie trailers using this application. The API will fetch the trailer from the internet and display it on the webpage. We will use 'movie-trailer' npm package
3 min read
Build a Health Tracker Using Next.js A Health Tracker application allows users to add and monitor various health metrics such as weight, exercise, sleep, etc., and monitor their progress over time. This article will guide you through building a Health Tracker web application with features for adding, viewing, and updating health metric
4 min read
Cryptocurrency Tracker with Next.js and API The cryptocurrency tracker is a web application built using NextJS that allows users to monitor the prices and other relevant information such as market cap, current price, total supply, and more for your favorite cryptocurrencies. The application provides a user-friendly interface for users to expl
5 min read