How to make a weather app using React Native
React Native is a dynamic cross-platform app development framework. If you are one of the developers who want to build a highly interactive app with less coding, React Native is the only solution. I have been using React Native for a while now and have fallen in love with the scalability and usability of this framework. In this article, I will show you how to make a fully running weather app using React Native app development services.
Here, I will use Expo CLI to create the weather app. This tool allows you to easily create a React Native app in minutes. If you want to learn more about Expo CLI and understand its differences from the bare React Native workflow, you should check out this article.
Creating a live weather forecasting app sounds interesting. Right? For this let us first check what prerequisites you should have.
- Firstly you should set up the React Native environment in your system. If you are unaware of the steps, check out the blog to get a detailed understanding.
- Create a minimalist React Native app with the Expo tool. For this, check out my blog on how to create and run React Native apps.
- Here, I will use the API Calling method to fetch live weather forecasting data. To understand how API calling and GET requests work, visit my blog on ‘How to make a GET request to an API from a React Native app’.
Note that I will be building the weather app for Android OS. If you want to build the app on iOS, you need to install Xcode. Since it requires a different approach, I will cover this topic in a different article.
So, let’s get started.
First I will create a minimalist React Native app using Expo. I will give it the name ‘weather-app’. Using the command
weather-app, I will create the app. Refer to image 1 for the app created on my local C drive.
Now I will start changing the loading screen and add different images and elements to the root directory of the app.
Building the loading screen
In this step, I will change the loading screen from ‘Open up App.js to start working on your app!’ to ‘Welcome to the Weather forecasting app.’.
For this, go to the app.js file of the app and change the
<Text>Open up App.js to start working on your app!</Text>to <Text>Welcome to the Weather
forecasting app</Text> Refer to image 2.
Refer to image 3 for the change in the output.
Using the isLoading state
- In this step, I will use the isLoading boolean state to define the local state as either true or false. With the practice of using isLoading, you can show whether the data that the app is trying to fetch is loading. You can set it as ‘true’ if it is loading data or else set it as ‘false’ if it is not.
- Here, in this project, I will show the text ‘Welcome to the Weather forecasting app.’ on the app screen when isLoading state is false. For this, consider the following code snippet. Refer to image 4 for the output.
- The main reason to use the isLoading state is to display the weather-based data in the place of the text ‘Welcome to the Weather forecasting app.’ after the API call is established successfully.
- As of now, I will design the code in a manner that the app will display the text ‘Please wait, while it is collecting the weather-based data.’ when the state isLoading is ‘true’. Considering the following code snippet. Refer to image 5 for the output.
- After the API fetched the data, you need to change the state of
Incorporating weather component
- In this part, I will show how you can design the body and the title of the first screen of your weather app.
- The title will comprise a small icon and the temperature. A weather-related text will be displayed on the remaining part of the screen.
- For this, you have to create a ‘Component’ folder in the root directory of your app. Then create a new file named ‘Weather.js’ in the Component folder. Here, you will design the code base of your first screen and add different weather components.
- After incorporating the main container, I will specify two containers namely titleContainer and bodyContainer in the main ‘Weather’ container.
- I will also use Feather from @expo/vector-icons directory to display an icon of snowflakes on the first screen of the app. This is one of the benefits that the Expo tool provides. It lets you add different libraries in the root directory of your project with zero hassle. Here, I have used the library named as vector-icons.
- Refer to the following code snippet and image 6 for the output.
API Calling- weather-based data
For fetching data from the web server, you can use the link https://openweathermap.org/. Also, you require a definite API key to call the data from a definite API URL. So, for this, you have to sign in on this website and go to the API section to get the ‘default’ key.
Below is the given code snippet of a weather app that you can use to build your understanding. Refer to image 7 for the output.
Code snippet for the weather app
Since I have used Expo, I ran the app on a real Android device using Expo Go. Open the Command prompt from your project and run the npm start. Try the codebase provided and modify the latitude and longitude of the location you want to display the temperature. You can also use this code snippet to add small icons per the weather conditions. Play around with the codebase and run it to see how a real-time weather app works.
A science graduate who has a keen interest to lean about new technologies and research area. With an experience in the field of data analytics and content writing, she aims to share her knowledge among passionate tech readers.