Shivam
Shivam

Reputation: 3131

Redux-saga: Error on configuring the store

I am trying to create a store and apply redux-saga middleware to it. I have configured every thing, But when I run the project, the following error pops up.

***Error: Before running a saga, you must mount the saga middleware on the store
using applyMiddleware

I error ocures on line sagaMiddleware.run(sagas);.

store.js

import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import sagas from '../redux/sagas';

const logger = createLogger({
  predicate: (getState, action) => isDebuggingInChrome,
  collapsed: true,
  duration: true,
  diff: true,
});

export default function configureStore() {
  const sagaMiddleware = createSagaMiddleware();
  const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

  const store = createStore(
    applyMiddleware(sagaMiddleware, logger),
  );

  sagaMiddleware.run(sagas);

  return store;
}

Any Idea what am I doing wrong?

react-native: 0.57.0
redux-saga: ^0.16.0
redux: ^4.0.0

Upvotes: 1

Views: 975

Answers (1)

Josep
Josep

Reputation: 13071

That's because you are not creating the store correctly, notice that you are not passing the rootReducer?

createStore is an enhancer that returns a function that takes the rootReducer as a parameter and the result of that is the store.

You probably want to do something like this, instead:

import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import sagas from '../redux/sagas';
import rootReducer from '../redux/rootReducer';

const logger = createLogger({
  predicate: (getState, action) => isDebuggingInChrome,
  collapsed: true,
  duration: true,
  diff: true,
});
const enhancers = process.env.NODE_ENV !== 'production' && window.devToolsExtension
  ? [window.devToolsExtension()]
  : [];

export default function configureStore() {
  const sagaMiddleware = createSagaMiddleware();

  const store = compose(
    applyMiddleware(sagaMiddleware, logger),
    ...enhancers
  )(createStore)(rootReducer);

  sagaMiddleware.run(sagas);

  return store;
}

Upvotes: 3

Related Questions