我在一个组件中发出异步请求,并将结果分派到存储。结果是对象数组。而不是在其他组件中,我尝试将其映射到标签中。
问题是我得到了类型错误,因为我的reducer的初始状态是null,并且无法映射null。尝试将其更改为仅为空数组,但得到相同的错误。
我的reducer:
const initialState = {
currentSearchResult: [],
};
const OrgsListReducer = (state = initialState, action) => action.type === 'GET_ORGS_LIST' ? action.payload : state;
有效载荷本身是正常的
我的组件:
class OrgList extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoaded: false,
orgList: this.props.currentSearchResult,
}
}
componentDidMount() {
if (this.props.currentSearchResult !== []) {
this.setState({
isLoaded: true
})
}
}
render() {
console.log(this.props.currentSearchResult);
return this.state.isLoaded ?
<ul>
{this.state.orgList.map((org) =>
<li key={org.id}>{org.login}</li>
)}
</ul>
:
<h1>Loading...</h1>
}
}
function mapStateToProps(state) {
return {
currentSearchResult: state.currentSearchResult
}
}
export default connect(mapStateToProps)(OrgList)
https://stackoverflow.com/questions/56762521
复制相似问题