繁体   English   中英

反应 redux 中的传播状态

[英]spread state in react redux

我想知道什么呢...state{ ...state }呢? 是将store的值改为初始值还是让store为最新值?

import * as actionType from "../actions/actionTypes";

const initialStore = {
  roomsCount: 0,
  resPerPage: 0,
  rooms: [],
  filteredRooms: 0,
  error: null,
  success: false,
};

const reducer = (state = initialStore, action) => {
  switch (action.type) {
    case actionType.ALL_ROOM_SUCCESS:
      return {
        ...state,
        success: true,
        rooms: action.rooms,
        roomsCount: action.roomsCount,
        resPerPage: action.resPerPage,
        filteredRooms: action.filteredRooms,
      };
    case actionType.ALL_ROOM_FAILED:
      return {
        ...state,
        error: action.err,
      };
  }
};

如果一开始我使用这个减速器,它会成功,所以success将为trueerror将为null 但是如果它第二次失败并且我在这种情况下使用...state ,那么成功值是多少? 它是初始值 ( false ) 还是保留第一个请求中的值 ( true )?

这称为扩展运算符,它基本上允许您将一个对象的字段“克隆”到一个新对象中。

在您的示例中, { ...state, error: action.err }表示“从state复制所有字段,但将字段error设置为action.err ”。 对于您想要更改很少字段但又想要保留原始数据的这种逻辑,它非常方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM