Skip to content

Commit

Permalink
[Examples] Remove getInitialProps from with-rematch (vercel#13834)
Browse files Browse the repository at this point in the history
Related to [11014](vercel#11014)
  • Loading branch information
todortotev authored and rokinsky committed Jul 11, 2020
1 parent b40acad commit 1069041
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 65 deletions.
21 changes: 8 additions & 13 deletions examples/with-rematch/pages/_app.js
@@ -1,17 +1,12 @@
import App from 'next/app'
import { Provider } from 'react-redux'
import { useStore } from '../shared/store'

import withRematch from '../shared/withRematch'
export default function App({ Component, pageProps }) {
const store = useStore(pageProps.initialReduxState)

class MyApp extends App {
render() {
const { Component, pageProps, reduxStore } = this.props
return (
<Provider store={reduxStore}>
<Component {...pageProps} />
</Provider>
)
}
return (
<Provider store={store}>
<Component {...pageProps} />
</Provider>
)
}

export default withRematch(MyApp)
32 changes: 31 additions & 1 deletion examples/with-rematch/shared/store.js
@@ -1,11 +1,14 @@
import { useMemo } from 'react'
import { init } from '@rematch/core'
import { counter, github } from './models'

let store

const exampleInitialState = {
counter: 5,
}

export const initializeStore = (initialState = exampleInitialState) =>
export const initStore = (initialState = exampleInitialState) =>
init({
models: {
counter,
Expand All @@ -15,3 +18,30 @@ export const initializeStore = (initialState = exampleInitialState) =>
initialState,
},
})

export const initializeStore = (preloadedState) => {
let _store = store ?? initStore(preloadedState)

// After navigating to a page with an initial Redux state, merge that state
// with the current state in the store, and create a new store
if (preloadedState && store) {
_store = initStore({
...store.getState(),
...preloadedState,
})
// Reset the current store
store = undefined
}

// For SSG and SSR always create a new store
if (typeof window === 'undefined') return _store
// Create the store once in the client
if (!store) store = _store

return _store
}

export function useStore(initialState) {
const store = useMemo(() => initializeStore(initialState), [initialState])
return store
}
51 changes: 0 additions & 51 deletions examples/with-rematch/shared/withRematch.js

This file was deleted.

0 comments on commit 1069041

Please sign in to comment.