javascript - react Redux : Using Immutable in Reduce State

标签 javascript reactjs redux react-redux immutable.js

我正在学习 Immutable.js,但我在 reducer 中处理 Immutable.js 时遇到困难。
我已经像这样声明了我的初始状态:

import { fromJS } from 'immutable';

const INITIAL_STATE = fromJS({
  users: {
    isLoading: false,
    items: []
  }
});  

我正在尝试修改初始状态,但收到错误:“state.setIn 不是函数”。

case 'FETCH_USERS_SUCCESS':
      return state
        .setIn(['users', 'isLoading'], false)
        .setIn(['users', 'items'], action.users)

在index.js 中,我将默认状态声明为 Immutable Map() 对象:

let store = createStore(..., Map({}), composeEnhancers(...));

在 mergeReducers 中,我使用“redux-immutable”。

import { combineReducers } from 'redux-immutable';  

使用 Immutable.js 修改 reducer 状态的正确方法是什么?

最佳答案

toJS 和 fromJS 是昂贵的操作。 您不应该执行initial_state 或state 的fromJS。

我建议您在用户 reducer 上执行此操作以获得更好的性能并且不要处理 toJS 和 fromJS。还可以在代码(选择器、组件、 View )中使用 get 来检索任何值。

import { List as ImmutableList, Map as ImmutableMap } from 'immutable';
import { FETCH_USERS_SUCCESS } from './constants'; // or whatever you have your constants file

const initialState = new ImmutableMap({
   isLoading: false,
   items: new ImmutableList([]);
});

function users(state = initialState, action) {
  switch (action.type):
   case FETCH_USERS_SUCCESS:
    return state.merge({
      isFetching: false,
      users: action.users
    });
   default:
    return state; 
}

请在创建 Store 时不要使用 immutable。您不需要任何不可变的操作,因为它应该是不可变的。 因此,Reducer 中的任何数据结构都应该是不可变的。

这是常见配置存储文件(或您的情况下的索引)的示例:

import { applyMiddleware, createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from 'state/reducers';

export default function configureStore(initialState = {}) {
  const store = createStore(
    rootReducer,
    initialState,
    composeWithDevTools(applyMiddleware(thunk)),
  );
  // to query state in the console
  window.appState = store.getState();

  if (module.hot) {
    const nextReducer = require('state/reducers');
    module.hot.accept('state/reducers', () => {
      store.replaceReducer(nextReducer);
    });
  }

  return store;
}

在文件状态/ reducer 中,您拥有不同实体的所有 reducer :

import { combineReducers } from 'redux';
import users from './users';

const ownReducers = {};

const appReducer = combineReducers({
  ...ownReducers,
  // add here your reducers after importing the entity state
  // i.e: myStuff: myStuff.reducer, etc...
  users: claims.reducer,
});

const rootReducer = (state, action) => {
  return appReducer(state, action);
};

export default rootReducer;

最好!

关于javascript - react Redux : Using Immutable in Reduce State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253701/

相关文章:

javascript - Offcanvas 菜单背景不透明

javascript - 加载在 JSBN 中创建的 RSA 公钥,然后加密一条消息

javascript - Jest 中的动态导入产生 : SyntaxError: Unexpected token import

javascript - 避免 NgRxReducer 中的状态突变

javascript - 状态改变不会导致渲染

reactjs - 为什么 Redux 将调度传递到操作的返回函数中?

javascript - 如果存储库中不存在文件,则终止 npm start 并显示自定义错误

javascript - 使用 jQuery 的模态窗口

javascript - 如何在blueprintjs v3中的 react 应用程序中为所有子组件切换主题(浅色和深色)?

javascript - Facebook React.js : How to use components directly inside html as normal tags without mounting?