Skip to main content

weather

Certainly! Here's a simple example of a weather widget in React with TypeScript. Since you won't be able to make API calls, I'll use a hardcoded data object to simulate the current weather information for a given location:

import React from 'react';

// Define the shape of the weather data using TypeScript interfaces
interface WeatherData {
location: string;
temperature: number;
description: string;
humidity: number;
windSpeed: number;
}

// Hardcoded weather data simulating an API response
const weatherData: WeatherData = {
location: 'San Francisco',
temperature: 16,
description: 'Partly cloudy',
humidity: 68,
windSpeed: 5
};

const WeatherWidget: React.FC = () => {
// Normally, you'd get this data from a state that is updated by an API call.
const { location, temperature, description, humidity, windSpeed } = weatherData;

return (
<div className="weather-widget">
<h2>Weather in {location}</h2>
<p>Temperature: {temperature}°C</p>
<p>Condition: {description}</p>
<p>Humidity: {humidity}%</p>
<p>Wind Speed: {windSpeed} km/h</p>
</div>
);
};

export default WeatherWidget;

This component would render a simple weather widget displaying the hardcoded data. In a real-world application, you would likely fetch this data from a weather API and update the component's state accordingly.

Here's what the component does:

  • Defines a WeatherData interface that describes the structure of the weather data we expect.
  • Uses a hardcoded weatherData object to simulate an API response.
  • Destructures the necessary fields from the weatherData object and displays them in the render method.

To fetch real weather data from an API, you'd typically use a service like OpenWeatherMap or similar. However, since I can't make external requests in this environment, I'll show you an example of how you would structure your component to make an API call. When you use this code in your own environment, you'll need to replace YOUR_API_KEY with your actual API key from the weather service you choose to use.

Here's a functional component example that would fetch weather data from an API:

import React, { useState, useEffect } from 'react';

// Define the shape of the weather data using TypeScript interfaces
interface WeatherData {
location: string;
temperature: number;
description: string;
humidity: number;
windSpeed: number;
}

// This is a mock function to simulate fetching weather data from an API.
// In a real application, you would make an actual network request here.
const fetchWeatherData = async (): Promise<WeatherData> => {
// Replace with your actual API call
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=San Francisco&appid=YOUR_API_KEY&units=metric`);
const data = await response.json();
return {
location: data.name,
temperature: data.main.temp,
description: data.weather[0].description,
humidity: data.main.humidity,
windSpeed: data.wind.speed
};
};

const WeatherWidget: React.FC = () => {
const [weather, setWeather] = useState<WeatherData | null>(null);

useEffect(() => {
const loadWeatherData = async () => {
try {
const data = await fetchWeatherData();
setWeather(data);
} catch (error) {
console.error("Error fetching weather data:", error);
}
};

loadWeatherData();
}, []);

// If the weather data hasn't been fetched yet, display a loading message
if (!weather) {
return <div>Loading...</div>;
}

// Display the weather data
return (
<div className="weather-widget">
<h2>Weather in {weather.location}</h2>
<p>Temperature: {weather.temperature}°C</p>
<p>Condition: {weather.description}</p>
<p>Humidity: {weather.humidity}%</p>
<p>Wind Speed: {weather.windSpeed} km/h</p>
</div>
);
};

export default WeatherWidget;

In this component:

  • We use the useState hook to create a weather state variable, which will store the weather data.
  • The useEffect hook is used to trigger the fetchWeatherData function when the component mounts.
  • The fetchWeatherData function (which you need to replace with a real API call) fetches the weather data and updates the weather state variable.
  • While the data is being fetched, the component renders a "Loading..." message.
  • Once the data is fetched, it is displayed in the component.

Remember to handle API keys securely; don't embed them directly in your front-end code. Instead, use environment variables or a backend service to keep them private.