Last active
February 11, 2019 20:54
-
-
Save bietkul/ade045f8b3a42fd179df06d6855d442d to your computer and use it in GitHub Desktop.
Create Redux store with Next.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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