React .map() 不是函数错误
React .map() is not a function error
import React, {Component} from 'react';
import './App.css';
import axios from 'axios';
export default class App extends Component {
constructor() {
super();
this.state = {
weather: []
};
}
componentDidMount (){
this.search();
}
search = () => {
axios.get("http://api.openweathermap.org/data/2.5/weather?q=Houston&APPID=f2327cca8f3d665f4c9f73b615b294ed")
.then(res => {
this.setState({weather: res.data});
}).catch(error => {
console.log('Error from fetching data', error);
})
}
render() {
console.log(this.state.weather);
const weathermap = this.state.weather.map( (maps) =>{
{maps.wind.speed}
});
return (
<div className="container">
<div className="row">
<div className="col-md-4 col-md-offset-4">
<div className="weather">
<div className="current">
<div className="info">
<div> </div>
<div className="city">
<small>
<small>CITY:</small>
</small>
{this.state.weather.name}</div>
<div className="temp">67°
<small>F</small>
</div>
<div className="wind">
<small>
<small>WIND:{weathermap}</small>
</small>
</div>
<div> </div>
</div>
<div className="icon">
<span className="wi-day-sunny"></span>
</div>
</div>
<div className="future">
<div className="day">
<h3>Mon</h3>
<p>
<span className="wi-day-cloudy"></span>
</p>
</div>
<div className="day">
<h3>Tue</h3>
<p>
<span className="wi-showers"></span>
</p>
</div>
<div className="day">
<h3>Wed</h3>
<p>
<span className="wi-rain"></span>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
我收到 "this.state.weather.map is not a function" 错误。我正在从天气 api 获取数据。我从 api 得到的名字显示正常。 api 自我称呼也可以成功。
api 在控制台中的样子
这是代码
您正在通过说 this.state = { weather: []};
来实例化应用程序。但是,当您说 this.setState({weather: res.data})
时,您会将 this.state.weather
覆盖为 JavaScript object
而不是 array
,因此 .map
不可用没有了。
您只需 const weathermap = this.state.weather.wind.speed
即可实现您的目标
this.state.weather 是一个 javascript 对象,所以 .map 不可用,您可以使用
Object.Keys(YourObject).map()
你应该做条件检查然后做.map like
.map with return 语法
const { weather } = this.state;
const weathermap = weather && weather.map(maps => {
return <span>{maps.wind && maps.wind.speed}</span>
});
.map 没有 return 语法
const { weather } = this.state;
const weathermap = weather && weather.map(maps => (
<span>{maps.wind && maps.wind.speed}</span>
));
import React, {Component} from 'react';
import './App.css';
import axios from 'axios';
export default class App extends Component {
constructor() {
super();
this.state = {
weather: []
};
}
componentDidMount (){
this.search();
}
search = () => {
axios.get("http://api.openweathermap.org/data/2.5/weather?q=Houston&APPID=f2327cca8f3d665f4c9f73b615b294ed")
.then(res => {
this.setState({weather: res.data});
}).catch(error => {
console.log('Error from fetching data', error);
})
}
render() {
console.log(this.state.weather);
const weathermap = this.state.weather.map( (maps) =>{
{maps.wind.speed}
});
return (
<div className="container">
<div className="row">
<div className="col-md-4 col-md-offset-4">
<div className="weather">
<div className="current">
<div className="info">
<div> </div>
<div className="city">
<small>
<small>CITY:</small>
</small>
{this.state.weather.name}</div>
<div className="temp">67°
<small>F</small>
</div>
<div className="wind">
<small>
<small>WIND:{weathermap}</small>
</small>
</div>
<div> </div>
</div>
<div className="icon">
<span className="wi-day-sunny"></span>
</div>
</div>
<div className="future">
<div className="day">
<h3>Mon</h3>
<p>
<span className="wi-day-cloudy"></span>
</p>
</div>
<div className="day">
<h3>Tue</h3>
<p>
<span className="wi-showers"></span>
</p>
</div>
<div className="day">
<h3>Wed</h3>
<p>
<span className="wi-rain"></span>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
我收到 "this.state.weather.map is not a function" 错误。我正在从天气 api 获取数据。我从 api 得到的名字显示正常。 api 自我称呼也可以成功。
api 在控制台中的样子
这是代码
您正在通过说 this.state = { weather: []};
来实例化应用程序。但是,当您说 this.setState({weather: res.data})
时,您会将 this.state.weather
覆盖为 JavaScript object
而不是 array
,因此 .map
不可用没有了。
您只需 const weathermap = this.state.weather.wind.speed
this.state.weather 是一个 javascript 对象,所以 .map 不可用,您可以使用
Object.Keys(YourObject).map()
你应该做条件检查然后做.map like
.map with return 语法
const { weather } = this.state;
const weathermap = weather && weather.map(maps => {
return <span>{maps.wind && maps.wind.speed}</span>
});
.map 没有 return 语法
const { weather } = this.state;
const weathermap = weather && weather.map(maps => (
<span>{maps.wind && maps.wind.speed}</span>
));