Blog

Build a Weather Application Using React

Jul 31, 2020 10 min read

Share Build a Weather Application Using React article on different platforms.

web-development

This weather application provides current and 24 hour 7-day weather forecast for any city in the world built with❤️using React. Weather forecast data is powered by Dark Sky and city search data is powered by Algolia Places.

Weather React Application UI

Weather React Application UI

🚀 Getting Started

Note: Dark Sky had been bought by Apple and is no longer accepting new signups. For more information, read this news here.

When you have a Dark Sky Account, follow the instructions described in-detail here or below to set up the project locally on your machine.

Basic Setup

  • Clone the local-setup branch in the repository
SHELL
git clone -b local-setup https://github.com/iamsainikhil/weather-react.git
  • Install the packages using the command npm install
  • Create a .env file in the root directory of the project. Add the following properties in it:
SHELL
REACT_APP_DARKSKY_API_KEY=<your Dark Sky API Key>
REACT_APP_ALGOLIA_PLACES_APP_ID=<your Algolia Places APP ID> (Optional)
REACT_APP_ALGOLIA_PLACES_API_KEY=<your Algolia Places Search-Only API Key> (Optional)

Algolia Places will be shutdown in 2022. For more information, read the announcement here.

  • You can obtain your Algolia Places API key here. You will get higher rate limits if you sign up and provide an api key OR it's just limited to 1000 requests/day! Moreover, code logic handles the scenario where no Algolia App ID & API key are provided since these are optional.

That's it! You can run the below available scripts to get up and running on the localhost. If you want to dive deeper into the codebase, I recommend you to check the architecture documentation to customize this application as your wish.

These instructions are very important to avoid the Blank Page issue when running the application on http://localhost:3000.

🧐 What's inside?

A quick look at the top-level files and directories in this weather-react project.

├── api
├── index.js
├── now.json
├── public
├── src
├── components
├── containers
├── context
├── fonts
├── sass
├── styles
├── tests
├── utils
├── App.js
├── index.js
├── serviceWorker.js
├── tailwind.config.js
  1. api: This directory contain all of the code related to the back-end of the application.
  2. public: This directory contain all the assets like images, icons, manifest (PWA), and favicons.
  3. src: This directory contain all of the code related to what you see on the front-end of the application. src is a convention for “source code”.
  4. tailwind.config.js: This file contains the configuration related to tailwindcss.

📖 Architecture

Weather React Application Architecture

Weather React Application Architecture

Weather React Application Architecture Breakdown

Weather React Application Architecture Breakdown

💻 Technologies

React – A JavaScript library for building user interfaces

A JavaScript library for building user interfaces

https://reactjs.org
React Logo
Create React App

Set up a modern web app by running one command.

https://create-react-app.dev
Create React App Logo
Tailwind CSS - A utility-first CSS framework for rapidly building custom designs

Documentation for the Tailwind CSS framework.

https://tailwindcss.com
Tailwind CSS

😢 Challenges

  • First, I used the OpenWeatherMap API to fetch the weather forecast data. However, 5-day forecast data was not reliable i.e. when a user on 14th March 2020 at 7:00 PM EST tries to fetch 5-day forecast data, will get forecast data starting 15th March 2020 at 12:00 AM UTC. This posed a big problem of categorizing 5-day data into individual days since the data is not always consistent and is based on UTC and not based on the user time-zone. Finally, I switched to Dark Sky API which is more reliable and provides a robust data model. However, there is a limit of 1000 calls/day.
  • Dark Sky API needs a proxy server to send and receive a response which was easy in the development stage using a browser extension like this to enable CORS in the browser. However, I can't ask every user to install this extension in their browser to check the weather forecast. So, I overcome this issue temporarily for now using the cors-anywhere library which you can get more info by checking here. However, I overcome the temporary solution and built a proxy server by deploying the Node.js  based serverless API functions on the Vercel's serverless architecture and can be accessed here.
  • Latest challenge I encountered was that Teleport API temporarily got shutdown and this led to broken autocomplete city search, and photos for favorited cities. Moreover, there is a tight coupling of code logic with this API. Now, I made a well thought highly scalable solution of using Algolia Places Rest API for fetching address based on city query as well as fetching city name based on latitude and longitude. I am very much happy about this change since it removed a lot of bad code and improved the application load times and performance.
    Note: The application UI/UX is not affected with this API change.
Rob--W/cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request. - Rob--W/cors-anywhere

https://github.com/Rob--W/cors-anywhere
cors-anywhere logo

👀 Demo

🏎 Roadmap

  •  Unit Testing
  •  Publish this project as an NPM package that can be consumed elsewhere.
  •  Add documentation for components using Storybook

🙌 Contribution

  • Open pull request with improvements.
  • If you have any new idea, check the feature request template to create a request.
  • If you found any issue or a bug, check the bug report template to create a report.

📃 License

Have a look at the license file for details

📧 Contact

Whether you’d like to discuss a project, ask me about my website or simply say “hello”, I’d love to hear from you.

Email: contact@iamsainikhil.com

😍 Featured On

A Nice Weather App Built Using React

A Nice Weather App Built Using React.

https://reactjsexample.com/a-nice-weather-app-built-using-react
A Nice Weather App Built Using React
Social Network for Programmers and Developers

Morioh is the place to create a Great Personal Brand, connect with Developers around the World and Grow your Career!

https://morioh.com/p/fc5f51d76847
Social Network for Programmers and Developers
This article was last updated on Dec 17, 2020

HTML

CSS

JavaScript

React

Documentation

ShareShare this article on different platforms.

Sai Nikhil - Photo

Sai Nikhil

A passionate developer who brings creative ideas from areas, including networking & data storage, security, UI/UX design, and progressive web applications with a strong understanding of the entire web development process.

Related Articles

NextJS Prismic Blog Starter

Build a Blog using NextJS & Prismic

Learn and build a blog using NextJS Prismic Blog starter template as quickl... Read Full Article

web-development

Dec 1, 2020| 15 min read

Prismic Webhooks

Prismic Webhooks

A Prismic Webhook allow you to automatically or manually trigger a deployme... Read Full Article

Oct 8, 2020| 3 min read

Theme UI - Themes

Build custom themes using Theme UI

Build themes for any JavaScript application using Theme UI. Read Full Article

Oct 3, 2020| 5 min read