Created useInjectReducer and useeInjectSaga hooks

This commit is contained in:
soupette 2019-07-04 09:37:33 +02:00
parent cc1dd40d82
commit c0a01f1e5a
5 changed files with 40 additions and 0 deletions

View File

@ -1,3 +1,8 @@
# getstarted
A quick description of getstarted.
Start the app with mongo
`DB=mongo yarn develop`

View File

@ -1,5 +1,7 @@
const injectReducer = require('./utils/injectReducer').default;
const useInjectReducer = require('./utils/injectReducer').useInjectReducer;
const injectSaga = require('./utils/injectSaga').default;
const useInjectSaga = require('./utils/injectSaga').useInjectSaga;
const { languages } = require('./i18n');
window.strapi = Object.assign(window.strapi || {}, {
@ -13,6 +15,8 @@ window.strapi = Object.assign(window.strapi || {}, {
'en',
injectReducer,
injectSaga,
useInjectReducer,
useInjectSaga,
});
module.exports = {

View File

@ -34,3 +34,14 @@ export default ({ key, reducer, pluginId }) => WrappedComponent => {
return hoistNonReactStatics(ReducerInjector, WrappedComponent);
};
const useInjectReducer = ({ key, reducer, pluginId }) => {
const context = React.useContext(ReactReduxContext);
const reducerName = pluginId ? `${pluginId}_${key}` : key;
React.useEffect(() => {
getInjectors(context.store).injectReducer(reducerName, reducer);
}, []);
};
export { useInjectReducer };

View File

@ -49,3 +49,19 @@ export default ({ key, saga, mode, pluginId }) => WrappedComponent => {
return hoistNonReactStatics(InjectSaga, WrappedComponent);
};
const useInjectSaga = ({ key, saga, mode, pluginId }) => {
const context = React.useContext(ReactReduxContext);
const sagaName = pluginId ? `${pluginId}_${key}` : key;
React.useEffect(() => {
const injectors = getInjectors(context.store);
injectors.injectSaga(sagaName, { saga, mode });
return () => {
injectors.ejectSaga(sagaName);
};
}, []);
};
export { useInjectSaga };

View File

@ -10,6 +10,8 @@ async function createPluginsJs(plugins, dest) {
const content = `
const injectReducer = require('./utils/injectReducer').default;
const injectSaga = require('./utils/injectSaga').default;
const useInjectReducer = require('./utils/injectReducer').useInjectReducer;
const useInjectSaga = require('./utils/injectSaga').useInjectSaga;
const { languages } = require('./i18n');
window.strapi = Object.assign(window.strapi || {}, {
@ -23,6 +25,8 @@ async function createPluginsJs(plugins, dest) {
'en',
injectReducer,
injectSaga,
useInjectReducer,
useInjectSaga,
});
module.exports = {