正确的方式附加到Redux State

时间:2016-07-26 08:05:05

标签: reactjs append redux state

我试图将数组附加到redux商店。

我称之为:

    if ($this->loginUser === null && $GLOBALS['TSFE']->loginUser && !empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
        // the user is logged in
    } else {
        // return '' as action content
        return '';
    }

行动创作者:

var hobbiesChosen = this.props.chosenHobbies;
for ( var i = 0; i < hobbiesChosen.length; i++) {
    var hobby = hobbiesChosen[i];
    this.props.fetchFilteredHobbies(hobby);
  }

我的减速机中有以下内容:

export function fetchFilteredHobbies(hobby) {
  return function(dispatch) {
      axios.get(`${API_URL}/hobbies/?hobby=${hobby}`, {withCredentials: true})
      .then(response => {
        dispatch({
          type:FILTERED_HOBBIES,
          payload: response
        });
      })
     .catch(() => {
        console.log("Not working");
      });
    }
}    

过滤的爱好多次运行,每次都会返回一个id列表。

  const INITIAL_STATE = { chosen:[]};

  export default function(state = INITIAL_STATE, action) {
        switch (action.type) {
          case FILTERED_HOBBIES:
            var filtered = _(action.payload.data).pluck('id').value();
            return { chosen: [...state.chosen, filtered] }

基本上,我想将action.payload的id附加到所选的reducer,但我不断收到此错误:

[1,2]

[5]

0 个答案:

没有答案