ReactJS,Redux:如何返回STATE中的一组项

ReactJS, Redux : How do I return an array of items in my STATE?

本文关键字:STATE 一组 返回 Redux 何返回 ReactJS      更新时间:2023-09-26

在从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,

更新我的组件。arrayId有2个项。

let arrayIds=[];
const FollowStatus = React.createClass ({
    componentWillMount(){
        arrayIds.push(this.props.status);
    },
    componentDidMount(){
        this.props.followCheckList(arrayIds)
    },

提前感谢各位

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