在通过redux从Mongodb Atlas提取文档(轴)时,我遇到了问题。我使用componentDidMount来解决获取和存储的异步时间,但由于“ this.props.cityList.map不是函数”,它在“ let listImage = this.props.cityList.map((i)...
”处显示错误
我将“ let listImage = this.props.cityList.map((i)...
”替换为“ console.log('city', this.props)
”,它会在控制台中正确显示所有必需的数据
下面是代码:
import React, {Component} from 'react';
import { connect } from 'react-redux';
import {bindActionCreators} from 'redux';
import {getCity} from '../actions/cityRead';
class DisplayCities extends React.Component {
componentDidMount() {
this.props.getCity()
}
cityLine() {
//console.log('city', this.props)
let listImage = this.props.cityList.map((i) =>{
return <img src={i.image} />;
});
return listImage;
}
render() {
return (
<div style={{marginTop: 10}}>
{this.cityLine()}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
cityList: state.cities
}
}
const mapDispatchToProps = (dispatch) => {
return bindActionCreators({getCity: getCity}, dispatch)
}
export default connect(mapStateToProps,mapDispatchToProps)(DisplayCities)
预期结果将显示所有通过“ map()
”的图像,但是会出现错误。非常感谢您的帮助,谢谢