Build a Weather Application Using React
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
SHELLgit 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:
SHELLREACT_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
- api: This directory contain all of the code related to the back-end of the application.
- public: This directory contain all the assets like images, icons, manifest (PWA), and favicons.
- 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”.
- tailwind.config.js: This file contains the configuration related to tailwindcss.
📖 Architecture
Weather React Application Architecture
Weather React Application Architecture Breakdown
💻 Technologies
- This application is created with Create React App
- React (v16.12)
- Styling syntax was written in SCSS and using TailwindCSS
React – A JavaScript library for building user interfaces
A JavaScript library for building user interfaces
https://reactjs.orgTailwind CSS - A utility-first CSS framework for rapidly building custom designs
Documentation for the Tailwind CSS framework.
https://tailwindcss.com😢 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👀 Demo
- https://iamsainikhil.com/weather-react
- API - https://weather-react-api.now.sh/
- GitHub - https://github.com/iamsainikhil/weather-react
🏎 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-reactSocial 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/fc5f51d76847Related Articles
Build a Blog using NextJS & Prismic
Learn and build a blog using NextJS Prismic Blog starter template as quickl... Read Full Article
Dec 1, 2020| 15 min read
Prismic Webhooks
A Prismic Webhook allow you to automatically or manually trigger a deployme... Read Full Article
Oct 8, 2020| 3 min read
Build custom themes using Theme UI
Build themes for any JavaScript application using Theme UI. Read Full Article
Oct 3, 2020| 5 min read