redux+immutable.js+redux-persist,state无法使用immutable方法

发布于 2022-09-11 23:46:15 字数 4304 浏览 27 评论 0

  • 报错信息

QQ截图20191127150149.png

  • 问题仓库重现链接,

redux与immutable.js和redux-persist,组件mapStateToProps中的state不是一个immutable对象

此时如何让mapStateToProps里的state变成immutable对象?
  • 项目目录
├── node_modules
├── package.json
├── public
├── README.md
├── src
    ├── App.js
    ├── index.js
    └── stor
        ├── actions.js
        ├── index.js
        ├── reducer.js
        └── state.js
└── yarn.lock
  • src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react';
import store, { persistor } from './store';
import App from './App';

ReactDOM.render(
  <Provider store={store}>
    <PersistGate persistor={persistor}>
      <App />
    </PersistGate>
    
  </Provider>, 
  document.getElementById('root')
);
  • src/App.js
import React from 'react';
import { connect } from 'react-redux';
// import {fromJS} from 'immutable';
class App extends React.Component {
  render() {
    const { a, b } = this.props;
    return (
      <div>
        <div style={{fontSize: '30px'}}>
          a= {a} --- b={b}
        </div>
      </div>
    )
  }
}
const mapStateToProps = state => {
  console.log(state);
  // 这里的state,不是一个immutable对象,难搞
  // state = fromJS(state)
  // console.log(state);
  return {
    a: state.getIn(["reducerA", "a"]),
    b: state.getIn(['reducerB', 'b'])
  }
  // return {
  //   a: state.reducerA.getIn(["a"]),
  //   b: state.reducerB.get('b')
  // }
}
export default connect(mapStateToProps, null)(App);
  • src/store/state.js
import { fromJS } from 'immutable';

const dataA = fromJS({
  a: 1
})
const dataB = fromJS({
  b: 2
})

export {
  dataA,
  dataB
}
  • src/store/actions.js
export const setA = a => { 
  return {
    type: 'SET_A',
    a
  }
}

export const setB = b => { 
  return {
    type: 'SET_B',
    b
  }
}
  • src/store/reducer.js
import { combineReducers } from 'redux';
import { dataA, dataB }  from './state';

const reducerA = (state = dataA, action) => {
  switch(action.type) {
    case 'SET_A':
      return state.set('a', action.a);
    default:
      return state;
  }
}

const reducerB = (state = dataB, action) => {
  switch (action.type) {
    case 'SET_B':
      return state.set('b', action.b);
    default:
      return state;
  }
}

const allReducer = {
  reducerA,
  reducerB
}

const rootReducer = combineReducers(allReducer);
export default rootReducer;

+src/store/index.js

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducer';
import { persistStore, persistReducer, } from 'redux-persist';
import immutableTransform from 'redux-persist-transform-immutable';
import storage from 'redux-persist/lib/storage';
// import autoMergeLevel2Immutable from './automergeLevel2-immutable';

const persistConfig = {
  key: 'root',
  storage,
  // stateReconciler: autoMergeLevel2Immutable,
  transforms: [immutableTransform()] // https://github.com/rt2zz/redux-persist-transform-immutable
}

const persistedReducer = persistReducer(persistConfig, rootReducer);

// https://github.com/zalmoxisus/redux-devtools-extension
const composeEnhancers =
  typeof window === 'object' &&
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
      // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
    }) : compose;

const enhancer = composeEnhancers(
  applyMiddleware(thunk),
  // other store enhancers if any
);

let store = createStore(persistedReducer, enhancer);

export const persistor = persistStore(store);
export default store;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2022-09-18 23:46:15
  1. https://github.com/rt2zz/redux-persist-transform-immutable
  2. https://segmentfault.com/q/1010000019280863?_ea=33859610

state顶层不能是immutable类型的数据的

建议使用immer.js代替immutable.js,更轻量,使用起来也更简单方便。只要简单的在reducer里进行修改就可以了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文