【发布时间】:2016-03-29 20:30:08
【问题描述】:
我无法从 api 调用返回的结果中取回 1 个数组项。
这是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;
})
}
}
}
我的减速器
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};
}
我期待来自 follow_check_list : action.result 的 2 个结果,所以在我的状态下我应该会看到
follow_check_list : Array[2].
0: item1,
1: item2
但是我看到了:
follow_check_list : Array[1].
0: item1,
更新 我的组件。 arrayIds 有 2 项。
let arrayIds=[];
const FollowStatus = React.createClass ({
componentWillMount(){
arrayIds.push(this.props.status);
},
componentDidMount(){
this.props.followCheckList(arrayIds)
},
提前谢谢大家
【问题讨论】:
-
“followers_checklist”应该来自哪里?
-
它在函数内部。如果它未定义,那么我创建它。我这样做是为了防止在多次调用函数时变量第二次实例化
-
你的“结果”在 AJAX 回调中是什么样子的?它是一个单一的对象吗?因为您所做的只是将一个对象推入数组...
-
是的,它是单个对象。我该怎么办?
-
好吧,如果您的响应是一项,那么您如何/为什么期望您调度的数组中有两项?就此而言,您的动作调度是什么样的?
标签: javascript reactjs redux react-redux