如何让react redux reducer处理CREATE动作

时间:2017-05-26 23:42:10

标签: reactjs redux react-redux

我的减速器目前正在正确加载猫,但没有正确添加猫。这是我的减速机:

import * as types from '../actions/actionTypes';

const initialState = []

export default function catReducer(state = initialState, action) {
  switch (action.type) {
    case types.LOAD_CATS_SUCCESS:
     return action.cats
    case types.CREATE_CAT_SUCCESS:
      return [
       ...state.filter(cat => cat.id !== action.cat.id),
       Object.assign({}, action.cat)
      ]
    default:
      return state;
  }
}

我是否错误地在CREATE_CAT_SUCCESS内添加了猫?

服务器正在使用以下内容响应create API帖子:

[{"name":"aXXXXX","id":3123123}]

API函数:

  static createCat(cat) {
    const request = new Request('http://localhost:4300/api/v1/skills', {
      method: 'POST',
      headers: new Headers({
        'Content-Type': 'application/json'
      }),
      body: JSON.stringify({cat: cat})
    });


    return fetch(request).then(response => {
      return response.json();
    }).catch(error => {
      return error;
    });
  }

谢谢

0 个答案:

没有答案