Skip to content

Instantly share code, notes, and snippets.

@azamatsmith
Last active May 23, 2021 11:38
Show Gist options
  • Save azamatsmith/ab814c869e81dc01b07782be0493ebcd to your computer and use it in GitHub Desktop.
Save azamatsmith/ab814c869e81dc01b07782be0493ebcd to your computer and use it in GitHub Desktop.
Adding Redux to Gatsby - with devtools and redux-thunk
import React from 'react';
import { Router } from 'react-router-dom';
import { Provider } from 'react-redux';
import { applyMiddleware, compose, createStore } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './src/reducers';
const loadDevTools = () =>
process.env.NODE_ENV === 'development' && window.devToolsExtension
? window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f;
const store = createStore(
rootReducer,
compose(applyMiddleware(thunk), loadDevTools())
);
exports.replaceRouterComponent = ({ history }) => {
const ConnectedRouterWrapper = ({ children }) => (
<Provider store={store}>
<Router history={history}>{children}</Router>
</Provider>
);
return ConnectedRouterWrapper;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment