react-redux中做异步添加reducer,但是无法通过context获取store,什么原因?

求帮助。
现在是想做异步加载模块,动态添加reducer,需要获取到根store
store.ts

import { createStore, combineReducers } from 'redux';

export function CreateStore(reducerMap: any) {

 console.log('reducerMap2',reducerMap);

 // @ts-ignore

 const injectAsyncReducers = (store, name, reducers) => {

 let asyncReducers;

 if (typeof reducers === "function") {

 asyncReducers = reducers;

 }

 if (typeof reducers === "object") {

 asyncReducers = combineReducers(reducers);

 }

 store.asyncReducers[name] = asyncReducers;

 store.replaceReducer(

 combineReducers({

 ...reducerMap,

 ...store.asyncReducers

 })

 );

 };

 const store = createStore(

 combineReducers(reducerMap),

 // @ts-ignore

 //   applyMiddleware(logger)

 );

 // @ts-ignore

 store.asyncReducers = {};

 // @ts-ignore

 store.addDynamicModule = ({ name, reducers }) => {

 console.info(`Registering module reducers for ${name}`);

 injectAsyncReducers(store, name, reducers);

 };

 // @ts-ignore

 store.removeDynamicModule = name => {

 console.info(`Unregistering module reducers for ${name}`);

 const noopReducer = (state = {}) => state;

 injectAsyncReducers(store, name, noopReducer);

 };

 return store;

}

但是我在lazyModule中无法获取到store

 static contextTypes = {

 // count: PropTypes.object,

 store: PropTypes.object

 }

也添加了
<Provider store={CreateStore(CounterReduce)}>

求帮助分析一下,怎么才能获取到store呢?

阅读 1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题