From ba54c6ac3d4f72428190f05ed833cfce9f0f5e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=AD=90=E5=A4=A7=E5=93=88?= Date: Sun, 19 Feb 2017 01:03:02 +0800 Subject: [PATCH] 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. --- examples/with-redux/README.md | 6 +++--- examples/with-redux/package.json | 2 +- examples/with-redux/pages/index.js | 8 ++++---- examples/with-redux/pages/other.js | 8 ++++---- examples/with-redux/store.js | 15 +++++++++++++-- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/examples/with-redux/README.md b/examples/with-redux/README.md index 93a1b04021..c8e60b897b 100644 --- a/examples/with-redux/README.md +++ b/examples/with-redux/README.md @@ -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) -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. diff --git a/examples/with-redux/package.json b/examples/with-redux/package.json index 6af311f24c..786266b682 100644 --- a/examples/with-redux/package.json +++ b/examples/with-redux/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "next": "^2.0.0-beta", - "next-redux-wrapper": "^1.0.0", + "next-connect-redux": "^0.1.1", "react": "^15.4.2", "react-dom": "^15.4.2", "react-redux": "^5.0.1", diff --git a/examples/with-redux/pages/index.js b/examples/with-redux/pages/index.js index 5408229f95..e699e272d8 100644 --- a/examples/with-redux/pages/index.js +++ b/examples/with-redux/pages/index.js @@ -1,11 +1,11 @@ import React from 'react' -import { reducer, initStore, startClock } from '../store' -import withRedux from 'next-redux-wrapper'; +import { nextConnect, reducer, startClock, setPageTitle } from '../store' import Page from '../components/Page' class Counter extends React.Component { static getInitialProps ({ store, isServer }) { store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() }) + store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Index Page' }) return { isServer } } @@ -19,9 +19,9 @@ class Counter extends React.Component { render () { return ( - + ) } } -export default withRedux(initStore)(Counter) \ No newline at end of file +export default nextConnect((state) => state)(Counter) diff --git a/examples/with-redux/pages/other.js b/examples/with-redux/pages/other.js index 459a657272..234b784a68 100644 --- a/examples/with-redux/pages/other.js +++ b/examples/with-redux/pages/other.js @@ -1,11 +1,11 @@ import React from 'react' -import { reducer, initStore, startClock } from '../store' -import withRedux from 'next-redux-wrapper'; +import { nextConnect, reducer, startClock, setPageTitle } from '../store' import Page from '../components/Page' class Counter extends React.Component { static getInitialProps ({ store, isServer }) { store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() }) + store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Other Page' }) return { isServer } } @@ -19,9 +19,9 @@ class Counter extends React.Component { render () { return ( - + ) } } -export default withRedux(initStore)(Counter) \ No newline at end of file +export default nextConnect((state) => state)(Counter) diff --git a/examples/with-redux/store.js b/examples/with-redux/store.js index a15a9f81ab..c695c375b1 100644 --- a/examples/with-redux/store.js +++ b/examples/with-redux/store.js @@ -1,13 +1,22 @@ import { createStore, applyMiddleware } from 'redux' 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) { - 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 } } +export const setPageTitle = (title) => { + return { + type: 'SET_PAGE_TITLE', + title + } +} + export const startClock = () => dispatch => { return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800) } @@ -15,3 +24,5 @@ export const startClock = () => dispatch => { export const initStore = (initialState) => { return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) } + +export const nextConnect = nextConnectRedux(initStore)