Intro
In this tutorial we are going to build a COVID or The Corona Virus Disease Tracker, a modern scalable React Application using React JS framework and four advanced React tools: Redux, Thunks, Selectors and Styled Components. Just React JS is enough for creating simple applications but if you want to build large, high performance applications, your job will be much more simplified if you know how to use these additional tools.
For the Application concept, I thought I would create something that might be useful for me in the current situation. The current situation in World is not good. COVID-19 or The Corona Virus Disease has affected lives of people in many countries. I am currently staying in Singapore and have been working from home since last two weeks. As a developer I thought it would be better to use my time during this weekend to create a Tutorial to build an App for tracking Corona Virus reports from different countries.
Demo:
http://bgwebagency.in/projects/ui/covid-tracker/
Features:
- Search Country Report: Country Codes you can use for testing: GB (United Kingdom), US (USA), SG (Singapore), GE (Georgia), IN (India), IT (Italy), ES (Spain).
- Pin Country Result to move a result to very top.
- Remove a Country from the list.
- Persisting the result on reload as well. Note: The results are not being saved in any Database. We will save them in localStorage since our only focus here is Frontend development with React.
Prerequisites for this Tutorial:
To get the most of this tutorial, it will be better if you already know the following:
- HTML, CSS
- Basic React (Optional but Helpful)
Softwares and APIs Required for this Tutorial:
- nodejs: https://nodejs.org/en/
- API: We are using an API which will return latest report for a country based on country code.
https://api.thevirustracker.com/free-api?countryTotal=US - More APIs from the same API provider:
https://thevirustracker.com/api - Alternate Similar APIs: (If the previous one is not working)
https://corona.lmao.ninja/v2/countries/SG
https://covid-19-apis.postman.com/
or any other API that returns report for a single country. - API for global stats: (For Task after Tutorial)
https://api.thevirustracker.com/free-api?global=stats - Alternate Similar API for global stats: (For Task after Tutorial)
https://corona.lmao.ninja/v2/all
What this Tutorial Will Cover?
- Introduction, and Project Setup:
We will setup the project and understand the project structure. - Build the Application view by creating components in React JS.
Important Sections: - Manage state of the application with Redux.
- Handle API/Asynchronous calls with Thunks.
- Selectors: A middle layer between API layer and Component View.
- Styled Components: For handling CSS in a smart way, from JS file instead of creating separate CSS file.
- Build app for Production deployment.
Why use the Advanced React Tools?
Every Application mainly consists of 3 important layers. API layer to get data from the APIs. A data layer where we can handle the data from API and modify it to our requirements, and last but not the least, a view layer to show the data.
React JS framework was basically designed to mainly take care of the views only. Which means React JS is powerful to show data but when it comes to the other tasks such as calling APIs and handling or managing data, although React can do the job, React is not that good. Because, React does not have any specific sets of standards on how to manage state and perform API calls etc. That is ok when we are creating a small application but if you are working on a large application with a team, each developer will have their own ways of handling the code. And thus not having a set of rules will clutter the code and it will be extremely difficult to debug the code in future. So that’s where all these tools are helpful. They provide extra sets of rules on how to do things. For example: Redux takes care of data or state management by adding some extra standard rules. Similarly Thunks have a standard way of calling the APIs. And styled components have a specific way of handling CSS.
So in summary, these extra tools help us organise the application in a much standard way by separating the responsibilities among different tools instead of handling everything with React. Thus, the Application is easy to manage and expand.
Tasks for you after Tutorial:
- Create Unpin Country Button, clicking which The pinned Countries can come back to the Not Pinned Countries Section.
- Create another React Component to show the Global Stats of total cases, from API: https://api.thevirustracker.com/free-api?global=stats. Use the same flow of first creating a GlobalStats.js component, add redux globalstats reducer, add selectors and finally adding styles with styled components.
- Modify the reducer code to remove isLoading reducer and add isLoading as a property of state.countries instead of direct child of the state. Because now we are adding a new API. and we will need isLoading property for individual API. So also we need to add another isLoading prop for state.globalstats.
Important links:
App Demo: http://bgwebagency.in/projects/ui/covid-tracker/
Github Project: https://github.com/kirandash/covid-tracker
Follow Me On Github: https://github.com/kirandash
Follow Me On Twitter: https://twitter.com/TheKiranDash