From 47e5231cdd86da28f9cdcef7c23ced86074d0814 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sat, 18 Feb 2017 14:11:54 -0300 Subject: [PATCH] Revert "Use the original idea of provider wrapper for redux example (#1201)" This reverts commit ba54c6ac3d4f72428190f05ed833cfce9f0f5e18. --- 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, 14 insertions(+), 25 deletions(-) diff --git a/examples/with-redux/README.md b/examples/with-redux/README.md index c8e60b89..93a1b040 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. +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. -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`. +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. -`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. +`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. 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 786266b6..6af311f2 100644 --- a/examples/with-redux/package.json +++ b/examples/with-redux/package.json @@ -8,7 +8,7 @@ }, "dependencies": { "next": "^2.0.0-beta", - "next-connect-redux": "^0.1.1", + "next-redux-wrapper": "^1.0.0", "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 e699e272..5408229f 100644 --- a/examples/with-redux/pages/index.js +++ b/examples/with-redux/pages/index.js @@ -1,11 +1,11 @@ import React from 'react' -import { nextConnect, reducer, startClock, setPageTitle } from '../store' +import { reducer, initStore, startClock } from '../store' +import withRedux from 'next-redux-wrapper'; 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 nextConnect((state) => state)(Counter) +export default withRedux(initStore)(Counter) \ No newline at end of file diff --git a/examples/with-redux/pages/other.js b/examples/with-redux/pages/other.js index 234b784a..459a6572 100644 --- a/examples/with-redux/pages/other.js +++ b/examples/with-redux/pages/other.js @@ -1,11 +1,11 @@ import React from 'react' -import { nextConnect, reducer, startClock, setPageTitle } from '../store' +import { reducer, initStore, startClock } from '../store' +import withRedux from 'next-redux-wrapper'; 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 nextConnect((state) => state)(Counter) +export default withRedux(initStore)(Counter) \ No newline at end of file diff --git a/examples/with-redux/store.js b/examples/with-redux/store.js index c695c375..a15a9f81 100644 --- a/examples/with-redux/store.js +++ b/examples/with-redux/store.js @@ -1,22 +1,13 @@ import { createStore, applyMiddleware } from 'redux' import thunkMiddleware from 'redux-thunk' -import nextConnectRedux from 'next-connect-redux' -export const reducer = (state = { lastUpdate: 0, light: false, title: '' }, action) => { +export const reducer = (state = { lastUpdate: 0, light: false }, action) => { switch (action.type) { - case 'TICK': return { lastUpdate: action.ts, light: !!action.light, title: state.title } - case 'SET_PAGE_TITLE': return { ...state, title: action.title } + case 'TICK': return { lastUpdate: action.ts, light: !!action.light } 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) } @@ -24,5 +15,3 @@ export const startClock = () => dispatch => { export const initStore = (initialState) => { return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) } - -export const nextConnect = nextConnectRedux(initStore)