首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Uncaught : this.props.forecast.map不是函数

Uncaught : this.props.forecast.map不是函数
EN

Stack Overflow用户
提问于 2021-12-24 17:31:14
回答 3查看 152关注 0票数 1

我是新的反应,看到了一些类似的问题,但还没有找到为什么会发生这种情况。我得到了“TypeError: this.state.data.map不是一个函数”。这是密码。请帮忙找出问题所在。

代码语言:javascript
复制
  import React from "react";
    
    class Forecast extends React.Component {
        render() {
            const forecastItems = this.props.forecast.map((f) => {
                const url = `http://openweathermap.org/img/wn/${f.wather[0].img}@2x.png`;
                let ampm = 'AM';
                let hour = new Date(f.dt * 1000).getHours();
    
                if (hour > 12) {
                    hour = hour - 12;
                    ampm = 'PM';
                }
                return (
                    <div className="forecast-item">
                        <p className="forecast-item__hour">{hour}:00 {ampm}</p>
                        <p className="forecast-item__temp">{f.temp}</p>
                        <img src={url} alt={f.weather[0].description}/>
                        <p className="forecast-item__description">{f.wather[0].main}</p>
    
                    </div>
                );
            });
            return (
                <div className="forecast">
                {forecastItems}
        </div>);
        }
    }
    
    export default Forecast;

这里是我的母公司,在这里我使用我的代码来获取天气api,它似乎做的一切都是正确的。

代码语言:javascript
复制
import React from "react";
import './App.css';
import SearchMenu from "./Components/search-menu/search-menu";
import CurrentWeather from "./Components/current-weather/current-weather";
import Forecast from "./Components/forecast-weather/forecast-weather";

import {getCurrentWather, getForecast} from './Components/api/weather.api';

class App extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            location: "",
            temp: "",
            feelsLike: "",
            description: "",
            img: "",
            hourlyForecast: ""
        };
    }

    onInputChange(e) {
        this.setState({
            location: e.target.value
        });
    }

    async onFormSubmit() {
      const weatherRes = await getCurrentWather(this.state.location);
      const lat = weatherRes.data.coord.lat;
      const lon = weatherRes.data.coord.lon;
      const forecastRes = await getForecast (lat,lon);

        this.setState({
            temp: weatherRes.data.main.temp,
            feelsLike: weatherRes.data.main.feels_like,
            description: weatherRes.data.weather[0].main,
            img: weatherRes.data.weather[0].img,
            hourlyForecast: forecastRes.data.hourly
                });
            }
    render() {
        return (
            <div className="App">
                <header className="App-header">
                    <SearchMenu
                        location={this.state.location}
                        inputChange={(e) => this.onInputChange(e)}
                        formSubmitted={() => this.onFormSubmit()}
                        />
                    <CurrentWeather
                        currentTemperature={this.state.temp}
                        feelsLike={this.state.feelsLike}
                        description={this.state.description}
                        img={this.state.img}
                    />
                    <Forecast forecast={this.state.hourlyForecast} />
                </header>
            </div>
        );
    }
    }


export default App;

EN

回答 3

Stack Overflow用户

发布于 2021-12-24 17:47:17

在构造函数中初始化状态,如下所示

代码语言:javascript
复制
...
this.state = {
            location: "",
            temp: "",
            feelsLike: "",
            description: "",
            img: "",
            hourlyForecast: []
        };

不同的是,hourlyForecast现在是一个空数组,而不是空字符串。

票数 0
EN

Stack Overflow用户

发布于 2021-12-24 17:49:54

在具有字符串初始值的父组件中为hourlyForecast设置状态,而不是数据数组。您应该将状态设置如下:

代码语言:javascript
复制
this.state = {
 ...
 hourlyForecast: []
};
票数 0
EN

Stack Overflow用户

发布于 2021-12-24 17:39:09

我认为下面应该是这样的,我猜您将hourlyForecast作为数组映射函数不理解并抛出类型错误的字符串传递。

代码语言:javascript
复制
import React from "react";
import './App.css';
import SearchMenu from "./Components/search-menu/search-menu";
import CurrentWeather from "./Components/current-weather/current-weather";
import Forecast from "./Components/forecast-weather/forecast-weather";

import {getCurrentWather, getForecast} from './Components/api/weather.api';

class App extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            location: "",
            temp: "",
            feelsLike: "",
            description: "",
            img: "",
            hourlyForecast: []
        };
    }

    onInputChange(e) {
        this.setState({
            location: e.target.value
        });
    }

    async onFormSubmit() {
      const weatherRes = await getCurrentWather(this.state.location);
      const lat = weatherRes.data.coord.lat;
      const lon = weatherRes.data.coord.lon;
      const forecastRes = await getForecast (lat,lon);

        this.setState({
            temp: weatherRes.data.main.temp,
            feelsLike: weatherRes.data.main.feels_like,
            description: weatherRes.data.weather[0].main,
            img: weatherRes.data.weather[0].img,
            hourlyForecast: Array.isArray(forecastRes.data.hourly) ? forecastRes.data.hourly : [forecastRes.data.hourly]
                });
            }
    render() {
        return (
            <div className="App">
                <header className="App-header">
                    <SearchMenu
                        location={this.state.location}
                        inputChange={(e) => this.onInputChange(e)}
                        formSubmitted={() => this.onFormSubmit()}
                        />
                    <CurrentWeather
                        currentTemperature={this.state.temp}
                        feelsLike={this.state.feelsLike}
                        description={this.state.description}
                        img={this.state.img}
                    />
                    <Forecast forecast={this.state.hourlyForecast} />
                </header>
            </div>
        );
    }
    }


export default App;
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70475314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档