-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0d3e85
commit 0ebca3d
Showing
5 changed files
with
89 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React from "react"; | ||
import {Provider} from "react-redux"; | ||
|
||
let memoizedStore; | ||
|
||
const initStore = (makeStore, isServer, initialState) => { | ||
|
||
// Always make a new store if server | ||
if (isServer && typeof window === 'undefined') { | ||
return makeStore(initialState); | ||
} | ||
|
||
// Memoize store if client | ||
if (!memoizedStore) { | ||
memoizedStore = makeStore(initialState); | ||
} | ||
|
||
return memoizedStore | ||
|
||
}; | ||
|
||
export default (createStore) => { | ||
|
||
return (Cmp) => { | ||
|
||
const WrappedCmp = ({initialState, isServer, store, ...props}) => { | ||
|
||
if (!store || !store.dispatch) { | ||
store = initStore(createStore, isServer, initialState); | ||
//console.log('4. WrappedCmp.render created new store with initial state', initialState); | ||
} else { | ||
//console.log('4. WrappedCmp.render picked up existing store'); | ||
} | ||
|
||
return ( | ||
<Provider store={store}> | ||
<Cmp {...props}/> | ||
</Provider> | ||
); | ||
|
||
}; | ||
|
||
/** | ||
* @param dialog | ||
* @return {{store, isServer: boolean, initialState}} | ||
*/ | ||
WrappedCmp.getInitialProps = async(dialog) => { | ||
|
||
const isServer = !!dialog.req; | ||
const store = initStore(createStore, isServer); | ||
|
||
// console.log('1. WrappedCmp.getInitialProps wrapper creates the store'); | ||
|
||
if (Cmp.getInitialProps) { | ||
await Cmp.getInitialProps.call(Cmp, {...dialog, isServer, store}); | ||
} | ||
|
||
// console.log('3. WrappedCmp.getInitialProps has store state', store.getState()); | ||
|
||
return { | ||
store, | ||
isServer, | ||
initialState: store.getState() | ||
}; | ||
|
||
}; | ||
|
||
return WrappedCmp; | ||
|
||
}; | ||
|
||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import Link from "next/link"; | ||
import React from "react"; | ||
import Layout from "../components/Layout"; | ||
import {makeStore} from "../lib/store"; | ||
import provide from "../lib/provide"; | ||
|
||
export default () => { | ||
return ( | ||
<Layout title="Other"> | ||
<h2>Other</h2> | ||
<nav> | ||
<Link href="/"><a>Navigate to index</a></Link> | ||
</nav> | ||
</Layout> | ||
) | ||
}; | ||
export default provide(makeStore)(() => ( | ||
<Layout title="Other"> | ||
<h2>Other</h2> | ||
<nav> | ||
<Link href="/"><a>Navigate to index</a></Link> | ||
</nav> | ||
</Layout> | ||
)); |