Creating a Weather App is a perfect real-world project to understand API integration in ReactJS. In this tutorial, youβll learn how to connect a React application with a public weather API, handle user input, and display dynamic weather data like temperature, location, and condition.
The OpenWeatherMap API, which is free and provides real-time weather information for a specific city, will be used. The Fetch API, useState, and useEffect will be combined to create a completely functional micro weather application from the ground up.
This project is great for practicing:
π Key Concepts:
- Handling user input through forms
- Managing async API calls with fetch()
- Displaying data conditionally
- Error handling and UX best practices
Letβs build it!
β Example: Simple Weather App using ReactJS
π§© React Functional Component (HTML + JavaScript)
π Note: Replace YOUR_API_KEY with your real OpenWeatherMap API key.
import React, { useState } FROM 'react';
FUNCTION WeatherApp() {
const [city, setCity] = useState('');
const [weather, setWeather] = useState(NULL);
const [error, setError] = useState('');
const fetchWeather = () => {
IF (!city) RETURN;
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=YOUR_API_KEY&units=metric`).then((res) => {
IF (!res.ok) throw NEW Error('City not found');
RETURN res.json();
})
.then((DATA) => {
setWeather(DATA);
setError('');
})
.catch((err) => {
setError('β Unable to fetch weather. Check city name.');
setWeather(NULL);
});
};RETURN (
<div STYLE={{ fontFamily: 'Arial', padding: '20px', maxWidth: '400px', margin: 'auto' }}>
<h2>π¦οΈ React Weather App</h2>
<INPUT
TYPE="text"
placeholder="Enter city name"
VALUE={city}
onChange={(e) => setCity(e.target.value)}
STYLE={{ padding: '10px', width: '70%' }}
/><button onClick={fetchWeather} STYLE={{ padding: '10px 15px', marginLeft: '10px' }}>
GET Weather</button>
{error && <p STYLE={{ color: 'red', marginTop: '10px' }}>{error}</p>}
{weather && (
<div STYLE={{ marginTop: '20px' }}>
<h3>{weather.name}, {weather.sys.country}</h3>
<p>π‘οΈ Temperature: {weather.main.temp} Β°C</p>
<p>βοΈ Condition: {weather.weather[0].description}</p>
</div>
)}
</div>
);}export DEFAULT WeatherApp;
π‘ Key Concepts Used:
- Input control using useState
- API call with fetch() and query parameters
- Conditional rendering for data and errors
- Use of OpenWeatherMap API with dynamic city input
This Weather App project shows how to use ReactJS to create a live, user-driven application that integrates with an external API. You discovered how to record user input, use that input to retrieve external data, and then dynamically display that data in the user interface. Features like temperature toggles (Β°C/Β°F), weather icons, auto-suggestions, and even map integrations can improve this even further!
How to integrate weather API in React JS?
Set up React project using the below command in VSCode IDE. Step 2: Navigate to the newly created project folder by executing the below command. Step 3: As we are using various packages for the project, we need to install them. Use the below command to install all the packages that are specified in package.
How to use API for weather app?
You must include an API key with every Weather API request. In the following example, replace YOUR_API_KEY with your API key. HTTPS is required for requests that use an API key.
What is the simplest weather API?
Visual Crossing Weather API is the easiest-to-use and lowest-cost weather API available. Our single-call API provides unified access to historical weather data, current conditions, forecasts, and climate statistics.
Related Topics | You May Also Like
|
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β |
