为了映射从promise返回的对象,我使用了react-router 4和redux,redux-promise。数据已经作为承诺返回,但我似乎无法映射它。如果我没记错的话,render()返回renderBanner函数并在那里呈现它,但是我有点不确定如何映射这个数据,因为我只想获取img并映射它。
编辑:示例(使用不同的api,但进程相同)
import React, { Component } from 'react';
import { Grid, Image } from 'react-bootstrap';
// import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { fetchAdverts } from '../actions/adverts_action';
class FullWidthBannerAd extends Component {
componentDidMount(props) {
this.props.fetchAdverts();
}
constructor(props) {
super(props)
// this.state = {
// data: []
// };
console.log('Props Contains:', props);
};
renderBanner(props) {
console.log('renderBanner Contains:', props);
return (
<div>
hello
</div>
)
}
render() {
return (
<Grid>
{this.renderBanner()}
</Grid>
);
}
}
function mapStateToProps(state) {
return {
adverts: state.adverts
};
}
export default connect(fetchAdverts)(FullWidthBannerAd);[,…]
0
:{_id: "57bf06e2ad5f52130098ae1c", sort_order: null, img: "e670cf43-9ed7-45f4-987d-d899af472f4c.jpg",…}
_id:"57bf06e2ad5f52130098ae1c"
img:"e670cf43-9ed7-45f4-987d-d899af472f4c.jpg"发布于 2017-04-04 05:28:08
import React, { Component } from 'react';
import { Grid, Image } from 'react-bootstrap';
// import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { fetchAdverts } from '../actions/adverts_action';
class FullWidthBannerAd extends Component {
constructor(props) {
super(props)
// this.state = {
// data: []
// };
console.log('Props Contains:', props);
};
componentDidMount() {
console.log(this.props);
this.props.fetchAdverts();
}
renderBanner() {
console.log('renderBanner Contains:', this.props);
return (
<div>
hello
</div>
)
}
render() {
return (
<Grid>
{this.renderBanner()}
</Grid>
);
}
}
将您的代码更改为以下代码。希望这能对你有所帮助
https://stackoverflow.com/questions/43184571
复制相似问题