Commit 71c47285 authored by March Works's avatar March Works Committed by Tim Neutkens

update with-rematch example (#6699)

Solved the following issue [#6686](https://github.com/zeit/next.js/issues/6686)
parent ad856eb2
...@@ -7,13 +7,13 @@ ...@@ -7,13 +7,13 @@
"start": "next start" "start": "next start"
}, },
"dependencies": { "dependencies": {
"@rematch/core": "0.6.0", "@rematch/core": "^1.1.0",
"isomorphic-unfetch": "2.0.0", "isomorphic-unfetch": "^3.0.0",
"next": "5.1.0", "next": "^8.0.3",
"react": "^16.7.0", "react": "^16.8.4",
"react-dom": "^16.7.0", "react-dom": "^16.8.4",
"react-redux": "5.0.7", "react-redux": "^6.0.1",
"redux": "3.7.2" "redux": "^4.0.1"
}, },
"license": "ISC" "license": "ISC"
} }
import React, { Component } from 'react' import React, { Component } from 'react'
import Link from 'next/link' import Link from 'next/link'
import { dispatch } from '@rematch/core' import { store } from '../shared/store'
import { initStore } from '../shared/store'
import withRematch from '../shared/utils/withRematch' import withRematch from '../shared/utils/withRematch'
import Header from '../shared/components/header' import Header from '../shared/components/header'
import CounterDisplay from '../shared/components/counter-display' import CounterDisplay from '../shared/components/counter-display'
...@@ -9,7 +8,7 @@ import CounterDisplay from '../shared/components/counter-display' ...@@ -9,7 +8,7 @@ import CounterDisplay from '../shared/components/counter-display'
class Github extends Component { class Github extends Component {
static async getInitialProps ({ isServer, initialState }) { static async getInitialProps ({ isServer, initialState }) {
if (isServer) { if (isServer) {
await dispatch.github.fetchUsers() await store.dispatch.github.fetchUsers()
} }
return {} return {}
} }
...@@ -54,4 +53,4 @@ const mapDispatch = ({ github: { fetchUsers } }) => ({ ...@@ -54,4 +53,4 @@ const mapDispatch = ({ github: { fetchUsers } }) => ({
fetchUsers: () => fetchUsers() fetchUsers: () => fetchUsers()
}) })
export default withRematch(initStore, mapState, mapDispatch)(Github) export default withRematch(store, mapState, mapDispatch)(Github)
import React, { Component } from 'react' import React, { Component } from 'react'
import { dispatch } from '@rematch/core' import { store } from '../shared/store'
import { initStore } from '../shared/store'
import withRematch from '../shared/utils/withRematch' import withRematch from '../shared/utils/withRematch'
import Header from '../shared/components/header' import Header from '../shared/components/header'
...@@ -13,7 +12,7 @@ class Home extends Component { ...@@ -13,7 +12,7 @@ class Home extends Component {
<h3>The count is {this.props.counter}</h3> <h3>The count is {this.props.counter}</h3>
<p> <p>
<button onClick={this.props.increment}>increment</button> <button onClick={this.props.increment}>increment</button>
<button onClick={() => dispatch.counter.increment(1)}> <button onClick={() => store.dispatch.counter.increment(1)}>
increment (using dispatch function) increment (using dispatch function)
</button> </button>
<button onClick={this.props.incrementBy(5)}>increment by 5</button> <button onClick={this.props.incrementBy(5)}>increment by 5</button>
...@@ -35,4 +34,4 @@ const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({ ...@@ -35,4 +34,4 @@ const mapDispatch = ({ counter: { increment, incrementAsync } }) => ({
incrementAsync: () => incrementAsync(1) incrementAsync: () => incrementAsync(1)
}) })
export default withRematch(initStore, mapState, mapDispatch)(Home) export default withRematch(store, mapState, mapDispatch)(Home)
import { init } from '@rematch/core' import { init } from '@rematch/core'
import { counter, github } from './models' import { counter, github } from './models'
// rematch store with initialValue set to 5 const initialState = {
export const initStore = (initialState = { counter: 5 }) => { counter: 5
return init({
models: {
counter,
github
},
redux: {
initialState
}
})
} }
export const store = init({
models: {
counter,
github
},
redux: {
initialState
}
})
...@@ -10,12 +10,12 @@ const checkServer = () => ...@@ -10,12 +10,12 @@ const checkServer = () =>
const getOrCreateStore = (initStore, initialState) => { const getOrCreateStore = (initStore, initialState) => {
// Always make a new store if server // Always make a new store if server
if (checkServer() || typeof window === 'undefined') { if (checkServer() || typeof window === 'undefined') {
return initStore(initialState) return initStore
} }
// Memoize store in global variable if client // Memoize store in global variable if client
if (!window[__NEXT_REMATCH_STORE__]) { if (!window[__NEXT_REMATCH_STORE__]) {
window[__NEXT_REMATCH_STORE__] = initStore(initialState) window[__NEXT_REMATCH_STORE__] = initStore
} }
return window[__NEXT_REMATCH_STORE__] return window[__NEXT_REMATCH_STORE__]
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment