mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
3db3f4b42a
It's better that Counter behave not only `increment (add)` but also `decrement` or `reset`. * Add decrement, reset handlers and rename add handler in `components/counter.js` * Add increment, decrement, reset actions to `store.js` * Rename page component class name: Counter => Index in `pages/index.js` * Remove needless dispatch count event on getInitialProps in `pages/index.js` * Format JSX to be readable in `pages/_app.js` * Remove needless line from `components/examples.js` * Remove needless spaces in `lib/with-redux-store.js`
50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
import App, {Container} from 'next/app'
|
|
import {Provider} from 'react-redux'
|
|
import {initializeStore} from '../store'
|
|
|
|
const isServer = typeof window === 'undefined'
|
|
const __NEXT_REDUX_STORE__ = '__NEXT_REDUX_STORE__'
|
|
|
|
function getOrCreateStore(initialState) {
|
|
// Always make a new store if server, otherwise state is shared between requests
|
|
if (isServer) {
|
|
return initializeStore(initialState)
|
|
}
|
|
|
|
// Store in global variable if client
|
|
if (!window[__NEXT_REDUX_STORE__]) {
|
|
window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
|
|
}
|
|
return window[__NEXT_REDUX_STORE__]
|
|
}
|
|
|
|
export default (App) => {
|
|
return class Redux extends React.Component {
|
|
static async getInitialProps (appContext) {
|
|
const reduxStore = getOrCreateStore()
|
|
|
|
// Provide the store to getInitialProps of pages
|
|
appContext.ctx.reduxStore = reduxStore
|
|
|
|
let appProps = {}
|
|
if (App.getInitialProps) {
|
|
appProps = await App.getInitialProps(appContext)
|
|
}
|
|
|
|
return {
|
|
...appProps,
|
|
initialReduxState: reduxStore.getState()
|
|
}
|
|
}
|
|
|
|
constructor(props) {
|
|
super(props)
|
|
this.reduxStore = getOrCreateStore(props.initialReduxState)
|
|
}
|
|
|
|
render() {
|
|
return <App {...this.props} reduxStore={this.reduxStore} />
|
|
}
|
|
}
|
|
}
|