mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Clean up Redux example (#4594)
- Remove unused imports - Edit some comments - Rename Wrapper App Class
This commit is contained in:
parent
4f4b7a1bce
commit
81802d2e60
|
@ -1,5 +1,4 @@
|
||||||
import App, {Container} from 'next/app'
|
import React from 'react'
|
||||||
import {Provider} from 'react-redux'
|
|
||||||
import {initializeStore} from '../store'
|
import {initializeStore} from '../store'
|
||||||
|
|
||||||
const isServer = typeof window === 'undefined'
|
const isServer = typeof window === 'undefined'
|
||||||
|
@ -11,7 +10,7 @@ function getOrCreateStore(initialState) {
|
||||||
return initializeStore(initialState)
|
return initializeStore(initialState)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store in global variable if client
|
// Create store if unavailable on the client and set it on the window object
|
||||||
if (!window[__NEXT_REDUX_STORE__]) {
|
if (!window[__NEXT_REDUX_STORE__]) {
|
||||||
window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
|
window[__NEXT_REDUX_STORE__] = initializeStore(initialState)
|
||||||
}
|
}
|
||||||
|
@ -19,16 +18,18 @@ function getOrCreateStore(initialState) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (App) => {
|
export default (App) => {
|
||||||
return class Redux extends React.Component {
|
return class AppWithRedux extends React.Component {
|
||||||
static async getInitialProps (appContext) {
|
static async getInitialProps (appContext) {
|
||||||
|
// Get or Create the store with `undefined` as initialState
|
||||||
|
// This allows you to set a custom default initialState
|
||||||
const reduxStore = getOrCreateStore()
|
const reduxStore = getOrCreateStore()
|
||||||
|
|
||||||
// Provide the store to getInitialProps of pages
|
// Provide the store to getInitialProps of pages
|
||||||
appContext.ctx.reduxStore = reduxStore
|
appContext.ctx.reduxStore = reduxStore
|
||||||
|
|
||||||
let appProps = {}
|
let appProps = {}
|
||||||
if (App.getInitialProps) {
|
if (typeof App.getInitialProps === 'function') {
|
||||||
appProps = await App.getInitialProps(appContext)
|
appProps = await App.getInitialProps.call(App, appContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue