Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Created November 13, 2018 13:22
Show Gist options
  • Save marcelaraujo/0dc718907675d965832a543f0dd704db to your computer and use it in GitHub Desktop.
Save marcelaraujo/0dc718907675d965832a543f0dd704db to your computer and use it in GitHub Desktop.
REACT & REDUX & ROUTER & HMR
// src/App.js
import React from 'react';
import {Provider} from 'react-redux';
import {ConnectedRouter} from 'react-router-redux';
import {history} from './history';
import {store} from './redux/store';
import {Root} from './components/Root';
export const App = () => <Provider store={store}>
<ConnectedRouter history={history}>
<Root/>
</ConnectedRouter>
</Provider>;
// src/history.js
import createHistory from 'history/createBrowserHistory'
export const history = createHistory();
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './App.js'; // check import path
const mountRoot = document.querySelector('#root'); // change this
if (process.env.NODE_ENV !== 'production') {
const AppContainer = require('react-hot-loader').AppContainer;
const render = Component => {
ReactDOM.render(
<AppContainer>
<Component/>
</AppContainer>,
mountRoot
);
};
render(App);
if (module.hot) {
module.hot.accept('./App.js', () => {// check import path
render(App);
});
}
} else {
ReactDOM.render(<App/>, mountRoot);
}
// src/redux/reducer.js
import {combineReducers} from 'redux';
import {routerReducer as router} from 'react-router-redux';
export const reducer = combineReducers({
router
});
// src/redux/saga.js
const sagas = [];
export const runAllSagas = middleware => sagas.forEach(saga => middleware.run(saga));
// src/redux/store.js
import {applyMiddleware, createStore} from 'redux';
import {routerMiddleware} from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import {history} from '../history';
import {reducer} from './reducer';
import {runAllSagas} from './saga';
const sagaMiddleware = createSagaMiddleware();
const enhancer = process.env.NODE_ENV !== 'production' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__(
applyMiddleware(sagaMiddleware, routerMiddleware(history))
)
: applyMiddleware(sagaMiddleware, routerMiddleware(history));
export const store = createStore(reducer, enhancer);
runAllSagas(sagaMiddleware);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment