Skip to content

Instantly share code, notes, and snippets.

@bietkul
Last active February 11, 2019 20:54
Show Gist options
  • Save bietkul/ade045f8b3a42fd179df06d6855d442d to your computer and use it in GitHub Desktop.
Save bietkul/ade045f8b3a42fd179df06d6855d442d to your computer and use it in GitHub Desktop.
Create Redux store with Next.js
import {
createStore, applyMiddleware, compose, combineReducers,
} from 'redux';
import thunkMiddleware from 'redux-thunk';
import reducer from '../modules/reducers';
let store = null;
const reducers = combineReducers(reducer);
const getStore = initialState => ({
store: createStore(reducers, initialState, compose(applyMiddleware(thunkMiddleware))),
});
export const initStore = (isServer, initialState) => {
if (isServer && typeof window === 'undefined') {
// Server
return getStore(initialState);
}
if (!store) {
// Client
store = getStore(initialState);
}
return store;
};
export default initStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment