在redux reducer中更新状态的正确方法

Max*_*cov 19 javascript flux reactjs redux

我是redux和es6语法的新手.我使用官方的redux教程制作我的应用程序,并使用此示例.

下面有JS片段.我的观点 - 在帖子reducer中定义REQUEST_POST_BODY和RECEIVE_POST_BODY个案.主要难点 - 在商店中查找和更新正确的对象.

我尝试使用示例中的代码:

  return Object.assign({}, state, {
    [action.subreddit]: posts(state[action.subreddit], action)
  })
Run Code Online (Sandbox Code Playgroud)

但它使用了简单的帖子.不需要通过id找到正确的帖子.

这是我的代码:

  const initialState = {
    items: [{id:3, title: '1984', isFetching:false}, {id:6, title: 'Mouse', isFetching:false}]
  }

  // Reducer for posts store
  export default function posts(state = initialState, action) {
    switch (action.type) {
    case REQUEST_POST_BODY:
      // here I need to set post.isFetching => true
    case RECEIVE_POST_BODY:
      // here I need to set post.isFetching => false and post.body => action.body
    default:
      return state;
    }
  }

  function requestPostBody(id) {
    return {
      type: REQUEST_POST_BODY,
      id
    };
  }

  function receivePostBody(id, body_from_server) {
    return {
      type: RECEIVE_POST_BODY,
      id,
      body: body_from_server
    };
  }

  dispatch(requestPostBody(3));
  dispatch(receivePostBody(3, {id:3, body: 'blablabla'}));
Run Code Online (Sandbox Code Playgroud)

Dan*_*nce 30

使用数组

如果您更喜欢坚持使用数组,那么您可以编写一个仅处理单个post对象的reducer .

export default function reducePost(post, action) {
  if(post.id !== action.id) return post;

  switch(action.type) {
  case REQUEST_POST_BODY:
    return Object.assign({}, post, { isFetching: true });
  case RECEIVE_POST_BODY:
    return Object.assign({}, post, { isFetching: false, body: action.body });
  default:
    return post;
}
Run Code Online (Sandbox Code Playgroud)

您的根减速器将变为:

export default function posts(state = initialState, action) {
  return state.map(post => reducePost(post, action);
}
Run Code Online (Sandbox Code Playgroud)

我们只是在列表中的每个帖子上运行我们的新reducer,以返回更新的帖子数组.在这种情况下,唯一ID将确保只更改一个项目.

用对象

如果每个项目都有唯一的字符串/数字ID,那么您可以翻转阵列并使用object替代.

const initialState = {
  items: {
    3: {id:3, title: '1984', isFetching:false},
    6: {id:6, title: 'Mouse', isFetching:false}
  };
}
Run Code Online (Sandbox Code Playgroud)

然后你可以简化你的减速机.

switch (action.type) {
case REQUEST_POST_BODY:
  let id = action.id;
  return Object.assign({}, state, {
    [id]: Object.assign({}, state[id], { isFetching: true })
  });
case RECEIVE_POST_BODY:
  let id = action.id;
  return Object.assign({}, state, {
    [id]: Object.assign({}, state[id], {
      isFetching: false,
      body: action.body
    })
  });
default:
  return state;
}
Run Code Online (Sandbox Code Playgroud)

如果您也很乐意尝试使用某些ES7语法,则可以使用Babel启用Object spread运算符并重写调用Object.assign.

switch (action.type) {
case REQUEST_POST_BODY:
  let id = action.id;
  return {
    ...state,
    [id]: {...state[id], isFetching: true }
  };
case RECEIVE_POST_BODY:
  let id = action.id;
  return {
    ...state,
    [id]: {
      ...state[id],
      isFetching: false,
      body: action.body
    }
  };
default:
  return state;
}
Run Code Online (Sandbox Code Playgroud)

如果你不是那么热衷于使用扩展语法,那么仍然可以使它Object.assign更加可口.

function $set(...objects) {
  return Object.assign({}, ...objects); 
}
case RECEIVE_POST_BODY:
  let id = action.id;
  return $set(state, {
    [id]: $set(state[id], {
      isFetching: false,
      body: action.body
    })
  });
Run Code Online (Sandbox Code Playgroud)