Learn React by building a Weather App

Build your first project with React

Prajwal
Webtips
6 min readJun 4, 2020

--

In my previous article, I had talked about react and its significance in the world of web application development. In this article, you’ll learn how to work with react by building a web application.

Getting Started

We’ve seen how to start a new project with react. So, lets get started by using the command: npx create-react-app weather-app .
Go into the project directory after installation using: cd weather-app.

Understanding the structure of your project

After executing the above command your project folder will look something like this:

We don’t have to worry about most of the files in this folder. All that matters is the index.html, App.js, index.css and index.js files. As of now the way things work is that all the things we want to render to the screen is written into App.js file(with the styles in index.css) which is passed onto the index.js that renders this App component in the div tag with the id ‘root’ in the index.html, which is displayed on the screen. Phew! Well, this is simply how react works, breaking the structure into components, using it wherever required, and passing it finally to the HTML file. Awesome!

The Weather App

Follow these steps to build a wonderful weather application:

  • Sign up at OpenWeatherMap and obtain the API keys.
  • Make a file called ‘keys.js’ in the src folder and use your API keys to place them in this file as mentioned below.
  • Copy-paste this piece of code into your App.js:
  • Then, copy this into your index.css file:

Explanation

I know you're overwhelmed by the code, but it will all be very clear once I break it down for you.

HTML: Only focus on the HTML elements of the App.js file. These HTML div’s include the search box, the location to be displayed, the temperature, and the prevalent conditions.

CSS: We will assign these div elements with a classname so that they can be given necessary styles in our CSS file.
(P.S: A little understanding of HTML and CSS is a pre-requisite.)

Date function: In the div class that handles the date we make sure to call a function called ‘dateBuild’. This ‘dateBuild’ function in javascript gets the date in string format. Then we extract the current month, date, and year using the slice() function.

API Fetch and Hooks: We make use of ‘useState’ Hook of react to change the state of what is being rendered on the screen. One hook for the search bar value and the other for the content being displayed.
In the div which handles the search bar, we call a function called ‘search’ when the user hits ‘Enter’ key. In this function, we make a call to the URL using our credentials and query to fetch the data from the server and then we change the state of what is being displayed on the screen. Hence, the useState hook is significant when it comes to state management in React.

Dynamic Background: This is just a simple logic to show the power of JSX in react. In my previous article, I had mentioned that JSX is an essential feature of react that combines the power of both javascript and HTML. This dynamic background feature is to make the application more stylish. In the class where we render the whole app, we just add a condition to check if the temperature fetched is greater than a certain limit. If yes, then add a different classname to the div tag that changes the background of the screen. Likewise, many such conditions can be applied to display a whole range of weather scenarios as background.

Result

If you have understood so far about how this application works, then go ahead and enter npm start into your terminal to get this app up and running.

Home Page
Hot weather page
Cold weather page

There you go, you just built an amazing weather application that keeps you informed of the weather conditions of cities thousands of miles away, right in your fingertips.

Thank you for reading this article, if you faced any issues feel free to contact me on LinkedIn. The entire project is available on GitHub.

Originally published at https://dev.to on June 4, 2020.

--

--

Prajwal
Webtips

Full-Stack Development and Data Science Enthusiast.