Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

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呢?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...