📅  最后修改于: 2023-12-03 15:04:50.574000             🧑  作者: Mango
React Weather App is a web application that allows users to retrieve weather information based on a chosen location. This application is built using Javascript, React, and external weather API.
The React Weather App is a single-page application that allows users to:
JavaScript is a popular programming language for building web applications and has been used to develop the React Weather App. JavaScript allows for the development of interactive and dynamic web pages, making it an excellent choice for front-end development.
React is a JavaScript library used for building user interfaces. React allows developers to create reusable UI components that can be combined to build complex interfaces. This feature is particularly useful when building single-page applications like the React Weather App.
The React Weather App uses the OpenWeatherMap API to retrieve weather data. The OpenWeatherMap API provides a vast amount of weather-related data, including current weather conditions, hourly forecasts, and 16-day forecasts, among others.
import React, { useState } from "react"
import axios from "axios"
const App = () => {
const [query, setQuery] = useState("");
const [weather, setWeather] = useState({});
const search = async (e) => {
if (e.key === "Enter") {
const data = await axios.get(
`https://api.openweathermap.org/data/2.5/weather?q=${query}&units=metric&APPID={API_KEY}`
);
setWeather(data.data);
setQuery("");
}
};
return (
<div>
<input
type="text"
className="search-bar"
placeholder="Enter City Name"
onChange={(e) => setQuery(e.target.value)}
value={query}
onKeyPress={search}
/>
{weather.main && (
<div>
<div className="city">{weather.name}</div>
<div className="temp">{Math.round(weather.main.temp)}°C</div>
<div className="weather">{weather.weather[0].description}</div>
<div className="humidity">
Humidity: {weather.main.humidity}%
</div>
<div className="wind-speed">
Wind Speed: {weather.wind.speed} meters/sec
</div>
</div>
)}
</div>
);
};
export default App;
The React Weather App is an excellent example of a single-page application built using Javascript and React. With its intuitive user interface, users can effortlessly obtain weather-related data, making it an invaluable tool for weather enthusiasts.