Reputation: 14363
I am trying to configure redux-saga
v1.1.3 in my application.
I did with the README here my configuration below.
In my project:
npm i [email protected] --save
I have created rootSaga.js
as follow:
import { all } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all([
...customSagas,
])
}
I turned the
rootSaga.js
into a function, because the rootSaga is within the core of my application and I needed to be able to add more customSaga to my application.
This is my updated configureStore.js
:
import { routerMiddleware } from 'connected-react-router';
import {
createStore,
applyMiddleware,
compose,
} from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
+ import createSagaMiddleware from 'redux-saga';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { augmentStore } from '@nfen/redux-reducer-injector';
import storage from './storage';
import combineAppReducers from '../reducers';
+ import rootSagas from '../sagas';
function setupCreateReducer(customReducers = {}, reducerConfig) {
return function createReducer() {
return combineAppReducers({
...customReducers,
}, reducerConfig);
};
}
export default function configureStore(customReducers, customSagas, initialState = {}, persistConfig = {}, reducerConfig = {}) {
const runtimePersistConfig = {
key: 'root',
stateReconciler: autoMergeLevel2,
storage,
...persistConfig,
whitelist: [
'preferences',
...(persistConfig.whitelist || []),
],
};
let composeEnhancers = compose;
const reduxSagaMonitorOptions = {};
/* istanbul ignore next */
if (__DEV__ && typeof window === 'object') { // eslint-disable-line no-undef
/* eslint-disable no-underscore-dangle */
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
}
/* eslint-enable */
}
+ const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
- const middlewares = combineAppMiddlewares([], reducerConfig.history);
+ const middlewares = combineAppMiddlewares([sagaMiddleware], reducerConfig.history);
- const middlewares = [routerMiddleware(reducerConfig.history)];
+ const middlewares = [sagaMiddleware, routerMiddleware(reducerConfig.history)];
const enhancers = [applyMiddleware(...middlewares)];
const createReducer = setupCreateReducer(customReducers, reducerConfig);
const persistedReducer = persistReducer(runtimePersistConfig, createReducer());
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(...enhancers),
);
// Extensions
+ store.runSaga = sagaMiddleware.run;
+ store.runSaga(rootSagas(customSagas));
augmentStore(createReducer, store);
const persistor = persistStore(store);
return { store, persistor };
}
In there, I use
redux
andredux-persist
, I also configure redux development tools. and have some core features such aspreferences
being preconfigured and persistent
This is my customSagas.js
:
import sessionsSaga from './sessionsSaga';
export default [
sessionsSaga,
];
This is my sessionSaga.js
:
import { takeLatest } from 'redux-saga/effects';
export function* logout() {
console.log('hello world');
}
export default function* logoutSaga() {
yield takeLatest('LOGOUT' , logout);
}
And this is how I call the saga in my JSX:
function App() {
return (
<Button
push={() => dispatch({ type: 'LOGOUT' })}
>
Logout Saga
</Button>
);
}
I can see that the LOGOUT
type is dispatched in my redux dev tools, but I can't see the hello world.
Where am I failing?
Upvotes: 0
Views: 95
Reputation: 36
when you do yield all([...])
in rootSaga
you have to call the functions like sessionSaga()
. in your code you are just listing the generator functions. see root saga docs
i think something like:
import { all, call } from 'redux-saga/effects'
export default (customSagas) => function* rootSaga() {
yield all(customSagas.map(saga => call(saga)));
}
could work.
Upvotes: 1