javascript 更新状态React Redux不可变

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 更新状态React Redux不可变相关的知识,希望对你有一定的参考价值。

export default function ActiveQueryReducer(_state=initialState,action={}){
  return _state
  .update(state =>
    state.update('filtered', ()=>
      //true if any root level filters and sorts are set (column filters, sorting, etc - anything not within a Query())
      !Boolean(
        state
        .keySeq()
        .toArray()
        .filter(key =>
          !['caseKinds',
            'fieldList',
            'didRun',
            'filtered',
            'lastState',
            'queries'
          ]
          .includes(key)
        )
        .every(key =>{
          return Boolean(state.get(key) === initialState.get(key))
        })
      )
      //true if there is more than one query
      || state.get('queries').size > 1
      //true if anything is set within the first Query()
      //note that itemKinds and itemTypes need to be filtered to remove their members with false values
      // i.e. if you click the Image filter button, itemKinds will be Map { image: true }
      // clicking the same button again results in itemKinds being Map { image: false }
      // removing these is necessary for comparison
      || !Boolean(
        state
        .getIn(['queries', 0])
        .update('itemKinds', kinds=>
          kinds.valueSeq().toArray().filter(val=>Boolean(val)).length === 0
          ? new Query().get('itemKinds')
          : kinds
        )
        .update('itemTypes', types=>
          types.valueSeq().toArray().filter(val=>Boolean(val)).length === 0
          ? new Query().get('itemTypes')
          : types
        )
        .entrySeq()
        .every(entry=>
          Boolean(entry[1] === new Query().get(entry[0]))
        )
      )
    )
  )
  .update(state => {

    switch(action.type){
      ...
    }
  });

}

以上是关于javascript 更新状态React Redux不可变的主要内容,如果未能解决你的问题,请参考以下文章

javascript 更新状态React Redux不可变

在React中使用Redux

如何使 thunk 独立于状态形状以使其可移植?

我想以不可变的方式更新状态对象,以便react和redux正常工作?

React 用于构建用户界面的 JavaScript 库

如何同步 Redux 状态和 url 哈希标签参数