ReactJS,Redux:如何在我的状态中返回一系列项目?

时间:2016-03-29 20:30:09

标签: javascript reactjs redux react-redux

我无法在api调用返回的结果中找回1个数组项。

这是行动中的功能。

  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)
    },

先谢谢你们

1 个答案:

答案 0 :(得分:1)

看起来你的followCheckList函数在“for”循环中有一个“return”语句:函数执行在第一次迭代时停止,这必须是你只得到“item1”的原因。