[英]ReactJS, Redux : How do I return an array of items in my STATE?
I'm having trouble getting back then 1 array item in the results returned from an api call. 我在从api调用返回的结果中返回1个数组项时遇到麻烦。
This is the function in actions. 这是行动中的功能。
followCheckList:function(userId){
for(var i= 0; i < userId.length;i++ ){
return{
types: [C.FOLLOW_CHECK_LIST, C.FOLLOW_CHECK_LIST_SUCCESS, C.FOLLOW_CHECK_LIST_FAIL],
promise: (client)=> client.get(`/graph/me/follows/${userId[i]}`)
.then(result => {
return result.id;
})
}
}
}
My reducers 我的减速机
case C.FOLLOW_CHECK_LIST:
return{
...state,
follow_check_list:null,
error:false,
};
case C.FOLLOW_CHECK_LIST_SUCCESS:
return{
...state,
//break here
error:false,
follow_check_list: action.result
};
case C.FOLLOW_CHECK_LIST_FAIL:
return{
...state,
follow_check_list:false,
error:action.error,
};
default: return state || {loading: false,
loaded: null,
list_loaded: null};
}
I'm expecting 2 results from follow_check_list : action.result
so in my state I should see 我期望来自
follow_check_list : action.result
2个结果follow_check_list : action.result
所以在我的状态下我应该看到
follow_check_list : Array[2].
0: item1,
1: item2
But instead I see : 但是我看到了:
follow_check_list : Array[1].
0: item1,
update My component. 更新我的组件。 arrayIds has 2 items.
arrayIds有2个项目。
let arrayIds=[];
const FollowStatus = React.createClass ({
componentWillMount(){
arrayIds.push(this.props.status);
},
componentDidMount(){
this.props.followCheckList(arrayIds)
},
Thanks in advance guys 在此先感谢大家
看起来您的followCheckList函数在“ for”循环中有一个“ return”语句:该函数的执行在第一次迭代时停止,这一定是为什么只得到“ item1”的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.