zappee
zappee

Reputation: 22646

how to create store with react-redux

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.

Upvotes: 6

Views: 18173

Answers (2)

Jalal Azimi
Jalal Azimi

Reputation: 87

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
});

Upvotes: 4

Jyothi Babu Araja
Jyothi Babu Araja

Reputation: 10282

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

Upvotes: 8

Related Questions