Use the original idea of provider wrapper for redux example (#1201)

Use the original idea of provider wrapper from #1193 and remove
unnecessary `initStore` in every page.
This commit is contained in:
胡子大哈 2017-02-19 01:03:02 +08:00 committed by Guillermo Rauch
parent 6268105807
commit ba54c6ac3d
5 changed files with 25 additions and 14 deletions

View file

@ -31,11 +31,11 @@ In this example we are going to display a digital clock that updates every secon
![](http://i.imgur.com/JCxtWSj.gif) ![](http://i.imgur.com/JCxtWSj.gif)
Our page is located at `pages/index.js` so it will map the route `/`. To get the initial data for rendering we are implementing the static method `getInitialProps`, initializing the redux store and dispatching the required actions until we are ready to return the initial state to be rendered. Since the component is wrapped with `next-react-wrapper`, the component is automatically connected to Redux and wrapped with `react-redux Provider`, that allows us to access redux state immediately and send the store down to children components so they can access to the state when required. Our page is located at `pages/index.js` so it will map the route `/`. To get the initial data for rendering we are implementing the static method `getInitialProps`, initializing the redux store and dispatching the required actions until we are ready to return the initial state to be rendered.
For safety it is recommended to wrap all pages, no matter if they use Redux or not, so that you should not care about it anymore in all child components. Connect every page to redux like you normally do using `nextConnect` function. `nextConnect` provides the same abilities of `connect`, in addition, it wraps every page with `react-redux Provider`. For safety it is recommended to wrap all pages, no matter if they use Redux or not, so that you should not care about it anymore in all child components. For normal components, you just need to use `connect` of `react-redux` instead of `nextConnect`.
`withRedux` function accepts `makeStore` as first argument, all other arguments are internally passed to `react-redux connect()` function. `makeStore` function will receive initialState as one argument and should return a new instance of redux store each time when called, no memoization needed here. See the [full example](https://github.com/kirill-konshin/next-redux-wrapper#usage) in the Next Redux Wrapper repository. `nextConnect` is generated by `nextConnectRedux`, which accepts `makeStore` as first argument, all other arguments are internally passed to `react-redux connect()` function. `makeStore` function will receive initialState as one argument and should return a new instance of redux store each time when called, no memoization needed here. See the [full example](https://github.com/huzidaha/next-connect-redux) in the `next-connect-redux` repository.
To pass the initial state from the server to the client we pass it as a prop called `initialState` so then it's available when the client takes over. To pass the initial state from the server to the client we pass it as a prop called `initialState` so then it's available when the client takes over.

View file

@ -8,7 +8,7 @@
}, },
"dependencies": { "dependencies": {
"next": "^2.0.0-beta", "next": "^2.0.0-beta",
"next-redux-wrapper": "^1.0.0", "next-connect-redux": "^0.1.1",
"react": "^15.4.2", "react": "^15.4.2",
"react-dom": "^15.4.2", "react-dom": "^15.4.2",
"react-redux": "^5.0.1", "react-redux": "^5.0.1",

View file

@ -1,11 +1,11 @@
import React from 'react' import React from 'react'
import { reducer, initStore, startClock } from '../store' import { nextConnect, reducer, startClock, setPageTitle } from '../store'
import withRedux from 'next-redux-wrapper';
import Page from '../components/Page' import Page from '../components/Page'
class Counter extends React.Component { class Counter extends React.Component {
static getInitialProps ({ store, isServer }) { static getInitialProps ({ store, isServer }) {
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() }) store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Index Page' })
return { isServer } return { isServer }
} }
@ -19,9 +19,9 @@ class Counter extends React.Component {
render () { render () {
return ( return (
<Page title='Index Page' linkTo='/other' /> <Page title={this.props.title} linkTo='/other' />
) )
} }
} }
export default withRedux(initStore)(Counter) export default nextConnect((state) => state)(Counter)

View file

@ -1,11 +1,11 @@
import React from 'react' import React from 'react'
import { reducer, initStore, startClock } from '../store' import { nextConnect, reducer, startClock, setPageTitle } from '../store'
import withRedux from 'next-redux-wrapper';
import Page from '../components/Page' import Page from '../components/Page'
class Counter extends React.Component { class Counter extends React.Component {
static getInitialProps ({ store, isServer }) { static getInitialProps ({ store, isServer }) {
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() }) store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Other Page' })
return { isServer } return { isServer }
} }
@ -19,9 +19,9 @@ class Counter extends React.Component {
render () { render () {
return ( return (
<Page title='Other Page' linkTo='/' /> <Page title={this.props.title} linkTo='/' />
) )
} }
} }
export default withRedux(initStore)(Counter) export default nextConnect((state) => state)(Counter)

View file

@ -1,13 +1,22 @@
import { createStore, applyMiddleware } from 'redux' import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk' import thunkMiddleware from 'redux-thunk'
import nextConnectRedux from 'next-connect-redux'
export const reducer = (state = { lastUpdate: 0, light: false }, action) => { export const reducer = (state = { lastUpdate: 0, light: false, title: '' }, action) => {
switch (action.type) { switch (action.type) {
case 'TICK': return { lastUpdate: action.ts, light: !!action.light } case 'TICK': return { lastUpdate: action.ts, light: !!action.light, title: state.title }
case 'SET_PAGE_TITLE': return { ...state, title: action.title }
default: return state default: return state
} }
} }
export const setPageTitle = (title) => {
return {
type: 'SET_PAGE_TITLE',
title
}
}
export const startClock = () => dispatch => { export const startClock = () => dispatch => {
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800) return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
} }
@ -15,3 +24,5 @@ export const startClock = () => dispatch => {
export const initStore = (initialState) => { export const initStore = (initialState) => {
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
} }
export const nextConnect = nextConnectRedux(initStore)