6

Sorry for the silly question but i do not know how to combine together my existing redux store definition with applyMiddleware.

This is my current working code:

const store = createStore(
  combineReducers({
    ...reducers,
    routing: routerReducer
  })
)

I would like to add this middleware somehow to my store definition:

applyMiddleware(...thunk)

My solution does not work, I get a "TypeError: dbg is undefined" in the web browser:

const store = createStore(
  applyMiddleware(...thunk),
  combineReducers({
    ...reducers,
    routing: routerReducer
  })
)

Could you please give me a quick help? Thank you.

zappee
  • 20,148
  • 14
  • 73
  • 129

2 Answers2

8

Try this

createStore(
  combineReducers({
    ...reducers,
    routing: routerReducer
  }), 
  applyMiddleware(thunk)
)

Syntax:

createStore(reducer, [preloadedState], [enhancer])

Enhancer must be the last parameter for createStore()

Read more from here

Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
4

better solution:

app.js:

    import {createStore, applyMiddleware, compose} from 'redux';
    import {browserHistory, Router, Route, IndexRoute} from 'react-router';
    import {syncHistoryWithStore, routerMiddleware} from 'react-router-redux';
    import {reducers} from './reducers';

   const initial_state = {};

    let middleware = applyMiddleware(routerMiddleware(browserHistory));
        if (process.env.NODE_ENV !== 'production') {
            middleware = compose(middleware, window.devToolsExtension && window.devToolsExtension());
        }
        const store   = createStore(reducers, initial_state, middleware);
        const history = syncHistoryWithStore(browserHistory, store);

reducers:

import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
import users from './users';

export const reducers = combineReducers({
    routing:routerReducer,
    users: users
});
Jalal Azimi
  • 87
  • 1
  • 4