Blog

Build a Weather Application Using React

Aug 11, 2020 10 min read

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

🚀 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 <code>npm install</code>
  • Create a <code>.env</code> 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)
  • 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.

Available Scripts

In the project directory, you can run:

npm run start

Runs the app in the development mode.Open http://localhost:3000/ to view it in the browser. The page will reload if you make edits. You will also see any lint errors in the console.

npm test

Launches the test runner in the interactive watch mode. See the section about running tests for more information.

npm run build

Builds the app for production to the build folder. It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes. Your app is ready to be deployed!

See the section about deployment for more information.

npm run eject

Notethis is a one-way operation. Once you eject, you can’t go back!

If you aren’t satisfied with the build tool and configuration choices, you can eject at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except eject will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use eject. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However, we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

Learn More

You can learn more in the Create React App documentation.

To learn React, check out the React documentation.

Code Splitting: For more info, check here: https://facebook.github.io/create-react-app/docs/code-splitting

Analyzing the Bundle Size: For more info, check here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

Making a Progressive Web App: For more info, check here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

Advanced Configuration: For more info, check here: https://facebook.github.io/create-react-app/docs/advanced-configuration

Deployment: For more info, check here: https://facebook.github.io/create-react-app/docs/deployment

<code>npm run build</code> fails to minify

💡 Motivation

I started learning React in February 2020 and thought of putting knowledge into reality by developing an application. I am interested in building an application that is API resource-intensive to challenge me writing efficient code keeping performance, UX, maintainability, scalability, and optimization in mind.

There are so many weather-related applications out in the wild. So, the goal is to create a UI that is beautiful yet simple and effective for any user to use.

💻 Technologies

React – A JavaScript library for building user interfaces

A JavaScript library for building user interfaces

https://reactjs.org
Create React App

Set up a modern web app by running one command.

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

Documentation for the Tailwind CSS framework.

https://tailwindcss.com
  • There are bunch of scripts that run when you start application to compile <code>scss</code> to <code>css</code>

😢 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 timezone. 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 API functions on the Vercel's serverless architecture and can be accessed here.
  • Latest challenge I encountered is that Teleport API is temporarily 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.

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

📖 Architecture

Weather React Application Architecture

❕ Detailed Architecture Documentation will be added soon...

🏎 Roadmap

  •  Unit Testing
  •  Publish this project as an NPM package that can be consumed elsewhere.
  •  Update Favorites UI
  •  Add documentation for components using Storybook
  •  Develop desktop application using Electron
  •  Develop Android and iOS app using React Native

🙌 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
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

🙏 Acknowledgements

See the Acknowledgements page on the wiki for a list of Acknowledgements for Weather React codebase.

👀 Demo

❕ Video will be added soon...

This article was last updated on Aug 28, 2020

ShareShare this article on different platforms.

Sai Nikhil

A passionate developer who brings creative ideas from areas, including networking and data storage, security, UI/UX design, and progressive web applications.

Related Articles

Embed GitHub Gist & CodeSandbox

This article demonstrates how to embed Github Gist and CodeSandbox using Em... Read More

Aug 22, 2020| 2 min read

Prismic Slices

This article demonstrates the different types of Prismic Slices used in thi... Read More

Aug 21, 2020| 4 min read