This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Babel for Fast Refresh
- @vitejs/plugin-react-swc uses SWC for Fast Refresh
The Weather component is a React-based widget that allows users to search for and display current weather information for a specified city. It utilizes the OpenWeatherMap API to fetch weather data and presents it in a user-friendly interface.
The component uses React's useState hook to manage two state variables:
city(string): Stores the user's input for the city name.weatherData(object | false): Stores the weather data fetched from the API. Initially set tofalse.
An asynchronous function that fetches weather data from the OpenWeatherMap API.
- Functionality:
- Constructs the API URL using the
citystate and an API key stored in the environment variableVITE_APP_ID. - Sends a GET request to the API.
- Parses the JSON response.
- Updates the
weatherDatastate with the fetched data.
- Constructs the API URL using the
- Error Handling: Logs any errors to the console.
-
Search Bar
- An input field for entering the city name.
- A search button with a magnifying glass icon.
-
Weather Icon
- Displays an icon representing the current weather condition.
- Currently shows a static "clear sky" icon.
-
Temperature Display
- Shows the current temperature in Celsius.
- Data source:
weatherData.main.temp
-
City Name Display
- Shows the name of the city.
- Data source:
weatherData.name
-
Weather Details Section
- Humidity:
- Displays humidity percentage.
- Data source:
weatherData.main.humidity
- Wind Speed:
- Displays wind speed in km/h.
- Data source:
weatherData.wind.speed
- Humidity:
- The component uses Tailwind CSS classes for styling.
- The layout is responsive, with a maximum width of 400px on medium-sized screens and above.
To use this component:
- Import it into your React application.
- Ensure you have the required environment variables set up, particularly
VITE_APP_IDfor the OpenWeatherMap API key. - Make sure the required icon assets are available in the specified paths.
- Add error handling for API request failures.
- Implement dynamic weather icons based on the actual weather condition.
- Add loading state indicators during API requests.
- Implement form submission on Enter key press.
- Add unit tests for component functionality.