Build a weather web application in React JS | React JS beginner Tutorial
Let's learn more about how all this works by building a weather application using React JS.
Installing Node and npm
Building a weather web application in React JS, we will have run-time environment called Node. It is mostly used to execute JavaScript code.
To download it, go to https://nodejs.org/en/.
You'll also need npm, which is a package manager built on Node. You can use it to install packages for your JavaScript apps. Fortunately it comes with Node, so you don't need to download it manually.
Once both of them are finished, open your terminal or command prompt and type node -v. and check npm -v .This checks which version of Node you have.
How to Create a React App
For creating our react application, just open your project directory and open the terminal and type npx create-react-app <Your app name> in your terminal, or npx create-react-app weather-app for this project.
You'll see that the packages are being installed.
Once the packages are done, go into the project folder by using cd weather-app and type npm start.
You'll see the default React js template, like this:
We don't need all of this right now. So, let's clear out some code.
In your App.js , App.css , index.css file, clear everything inside the div tag. Remove the logo import and delete all css in css files.
You will receive a blank screen on the output once you have done that.
How to Install the Packages We Need
To make this application more attractive, we need some external packages. So, let's install them.We need moment.js to format our time. Install it using the following command:
npm install moment --save
You can check your package.json file to keep track of all the installed packages.
You can check your package.json file to keep track of all the installed packages.
How to Create Our Weather Application
To make our weather application work, we need OpenWeatherMap, a third-party API that'll let us fetch the weather data.
Go to https://home.openweathermap.org/users/sign_up and create your own account.
After you are done, click on the API option on the Navigation bar. You'll see different options like Current Weather Data, Hourly 4 hour forecasts, 16 day forecasts, and others. These are API endpoints that you'll need to fetch the data.
How to Fetch Weather API Data
To fetch weather api data we just request using the fetch in react js using useEffect
then store the data in useState variable in json format first we will fetch the current weather data of that city and then after fecth forecast data of the city if city is available then data will be consoled on browser otherwise it will alert that city not found.
Let's Do Some Styling
Now that we have all our data, let's style them to make it more attractive.
Let's Recap What We've Done
We have created a React application that shows the current weather using the openweathermaps api based on your city and we just got current weather data forecast data as well and we have used moment.js to make the format the timestamp an we also used react hooks and useEffect .
That's all. You can add more features to the app, like in style or as well as there are a lot more feature in weather API like a five-day forecast, icons, and more.
You can find the code on Github if you want to experiment further.
Comments
Post a Comment