redux的reducer纯函数处理state的2种方法

1、使用lodash的_.cloneDeep()函数
深复制,可以递归复制复杂的对象,Object.assign()和_.assign()只会复制对象的第一层属性,更深层次的属性是引用赋值的,指向同一对象

const newState = cloneDeep(state)
newState.name = action.name
return newState

2、使用官方提供的update

import update from 'react-addons-update';

update(state, {
        name: action.name,
      })

下面看下update是如何工作的

function update(object, spec) {
    if (!(Array.isArray(object) && Array.isArray(spec))) {
      invariant(
        !Array.isArray(spec),
        'update(): You provided an invalid spec to update(). The spec may ' +
        'not contain an array except as the value of $set, $push, $unshift, ' +
        '$splice or any custom command allowing an array value.'
      );
    }

    invariant(
      typeof spec === 'object' && spec !== null,
      'update(): You provided an invalid spec to update(). The spec and ' +
      'every included key path must be plain objects containing one of the ' +
      'following commands: %s.',
      Object.keys(commands).join(', ')
    );

    var nextObject = object;

    var specKeys = getAllKeys(spec);
    var index, key;
    // 遍历 属性变化对象的属性
    for (index = 0; index < specKeys.length; index++) {
      key = specKeys[index];
      if (hasOwnProperty.call(commands, key)) {
        nextObject = commands[key](spec[key], nextObject, spec, object);
      } else {
      // 递归更新更深层次的对象
        var nextValueForKey = update(object[key], spec[key]);
        if (nextValueForKey !== nextObject[key]) {
          if (nextObject === object) {
            nextObject = copy(object);
          }
          nextObject[key] = nextValueForKey;
        }
      }
    }
    return nextObject;
  }

}

第二种方法相对于前者来说不需要深复制state对象,redux是根据newState === state判断state是否改变的,所以reducer必须生成一个新的对象

Redux是一个用于JavaScript应用程序的可预测状态容器,它可以帮助我们更好地管理应用程序的状态。而Reducer是Redux中用于处理状态变化的函数,它接收先前的状态和一个动作对象作为参数,并返回新的状态。Reducer的设计灵感来自于JavaScript中的Array.prototype.reduce()方法,因为它们都是将先前的值和当前的值组合成一个新值的过程。在Redux中,Reducer通常是函数,它们不会修改先前的状态,而是返回一个新的状态对象。 下面是一个简单的Redux应用程序,其中包含一个Reducer: ```javascript // 定义初始状态 const initialState = { count: 0 }; // 定义Reducer function counterReducer(state = initialState, action) { switch (action.type) { case 'INCREMENT': return { count: state.count + 1 }; case 'DECREMENT': return { count: state.count - 1 }; default: return state; } } // 创建Redux store const { createStore } = Redux; const store = createStore(counterReducer); // 订阅状态变化 store.subscribe(() => { console.log(store.getState()); }); // 分发动作 store.dispatch({ type: 'INCREMENT' }); // 输出:{ count: 1 } store.dispatch({ type: 'INCREMENT' }); // 输出:{ count: 2 } store.dispatch({ type: 'DECREMENT' }); // 输出:{ count: 1 } ``` 在上面的例子中,我们定义了一个名为`counterReducer`的Reducer函数,它接收先前的状态和一个动作对象作为参数,并返回一个新的状态对象。我们还创建了一个Redux store,并将`counterReducer`作为参数传递给`createStore`函数。最后,我们分发了一些动作,并在控制台中打印了状态变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值