Skip to content

Instantly share code, notes, and snippets.

@abstractmachines
Last active January 9, 2021 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractmachines/2dde39cb1dc47ea2ab3e61b86e76f995 to your computer and use it in GitHub Desktop.
Save abstractmachines/2dde39cb1dc47ea2ab3e61b86e76f995 to your computer and use it in GitHub Desktop.
Redux Devtools Recipes: composeEnhancers, saga middleware, and TypeScript

Redux Devtools Recipes

With composeEnhancers, Router:

const reduxDevToolsCompose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
	trace: true,
	traceLimit: 25,
});

const composeEnhancers = reduxDevToolsCompose || compose;

const store = createStore(
    connectRouter(history)(combineReducers({ ...reducers })),
    composeEnhancers(applyMiddleware(...middleware))
  );

With TS, sagas:

import {applyMiddleware, compose, createStore} from 'redux'
import rootReducer from './reducers/rootReducer'
import createSagaMiddleware from 'redux-saga'
import rootSaga from "./sagas/sagas";

const sagaMiddleware = createSagaMiddleware()

// Add property to global window so TS recognizes it
declare global {
    interface Window {
        __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
    }
}

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const enhancer = composeEnhancers(
    applyMiddleware(sagaMiddleware)
)

const store = createStore(
    rootReducer,
    enhancer
)

sagaMiddleware.run(rootSaga)

export default store

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment