You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将异步请求getWeather的数据传递回FiveDayWeather函数组件?

Ah, I see the issue here—you're running into the classic async/React state problem! Let's break this down and fix it step by step.

Why Your Current Code Isn't Working

First, let's clarify the core problem:

  • getWeather() is asynchronous: axios.get returns a Promise, but your function doesn't return that Promise. So let days = getWeather() will only ever give you undefined.
  • Even if you returned the Promise, days would be a Promise object—not the actual weather data—and you can't directly render Promises in JSX.
  • React function components rely on state to track dynamic data. When state updates, the component re-renders with the new data—this is the right way to handle async results in React.

The Fix: Use React Hooks (useState + useEffect)

Here's how to adjust your code to properly fetch and use the weather data:

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

const URL = `http://api.openweathermap.org/data/2.5/forecast?zip=94040&appid=${OPEN_WEATHER_KEY}`;

const FiveDayWeather = () => {
  // State to hold weather data, loading status, and error messages
  const [weatherData, setWeatherData] = useState(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  // Fetch data when the component first mounts
  useEffect(() => {
    const fetchWeather = async () => {
      try {
        const response = await axios.get(URL);
        // Store the fetched data in state
        setWeatherData(response.data);
      } catch (err) {
        setError('Failed to load weather data');
        console.error('Weather fetch error:', err);
      } finally {
        // Turn off loading state once the request finishes
        setLoading(false);
      }
    };

    fetchWeather();
  }, []); // Empty dependency array = run only once on component mount

  // Handle loading/error states first
  if (loading) {
    return <div>Loading 5-day forecast...</div>;
  }

  if (error) {
    return <div className="error-message">{error}</div>;
  }

  // Make sure we have data before rendering
  if (!weatherData) {
    return <div>No weather data available</div>;
  }

  // Process the API data (adjust based on OpenWeatherMap's response structure)
  const dailyForecasts = weatherData.list; // This gives 3-hour intervals—you may want to group by day

  return (
    <div className="five-day-weather">
      {dailyForecasts.map((forecast, index) => (
        <div key={index} className="day-of-week-item">
          <div className="day">
            {new Date(forecast.dt_txt).toLocaleDateString('en-US', { weekday: 'long' })}
          </div>
          <div className="image-indicator">
            <img
              src={`http://openweathermap.org/img/w/${forecast.weather[0].icon}.png`}
              alt={forecast.weather[0].description}
            />
          </div>
          <div className="temp">{Math.round(forecast.main.temp - 273.15)}°C</div>
        </div>
      ))}
    </div>
  );
};

Key Concepts Explained

  • useState: This hook lets you store dynamic data in your component. When you call setWeatherData, React re-renders the component with the new data.
  • useEffect: This hook runs side effects (like async requests) in function components. The empty dependency array ensures the request only runs once when the component mounts.
  • Async/Await: A cleaner alternative to .then() for handling Promises—though you could still use .then() if you prefer.
  • Conditional Rendering: We handle loading, error, and empty data states to avoid rendering errors and improve user experience.

About That Callback Advice You Saw

The callback approach you read about translates directly to updating state here. When the async request finishes, you use the callback (either .then() or the code after await) to update your component's state. React takes care of propagating that data to your JSX automatically—you don't need to "pass it back up the call chain" because the component will re-render with the new state.

内容的提问来源于stack exchange,提问作者adamscott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:19:40