S. Schenk
S. Schenk

Reputation: 2150

Configure a redux store with redux devtools (the chrome extension)

I am using the following boilerplate example and I'm trying to configure it to work with the chrome extension for redux devtools:

import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import apiMiddleware from '../middleware/api'
import createLogger from 'redux-logger'
import rootReducer from '../reducers'


const logger = createLogger({
  level: 'info',
  collapsed: false,
  logger: console,
  predicate: (getState, action) => true
})

const createStoreWithMiddleware = applyMiddleware(
  thunkMiddleware,
  apiMiddleware,
  logger
)(createStore)

export default function configureStore(initialState) {
  const store = createStoreWithMiddleware(rootReducer, initialState)

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers')
      store.replaceReducer(nextRootReducer)
    })
  }

  return store

}

I've tried adding it as follows, but I'm getting a window not defined error:

import { compose, createStore, applyMiddleware } from 'redux'

const createStoreWithMiddleware = compose(applyMiddleware(
  thunkMiddleware,
  apiMiddleware,
  logger
),window.devToolsExtension ? window.devToolsExtension() : f => f)(createStore)

I'm assuming the structure is somehow different, than the one given in the reudx-devtools extension example pages, but I can't put my finger on it.

How do I setup the store with middlewares and enhancements the right way?

Upvotes: 2

Views: 3807

Answers (2)

Naved Khan
Naved Khan

Reputation: 1947

import {createStore, applyMiddleware, compose} from 'redux'

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
    
const store = createStore(reducers, composeEnhancers(applyMiddleware()))

Upvotes: 0

xiaofan2406
xiaofan2406

Reputation: 3310

typeof window === 'object' && typeof window.devToolsExtension !== 'undefined'
  ? window.devToolsExtension()
  : f => f

This should fix the error.

Upvotes: 2

Related Questions