Skip to content

Commit 47e5231

Browse files
committed
Revert "Use the original idea of provider wrapper for redux example (vercel#1201)"
This reverts commit ba54c6a.
1 parent ba54c6a commit 47e5231

5 files changed

Lines changed: 14 additions & 25 deletions

File tree

examples/with-redux/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ In this example we are going to display a digital clock that updates every secon
3131

3232
![](http://i.imgur.com/JCxtWSj.gif)
3333

34-
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.
34+
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.
3535

36-
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`.
36+
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.
3737

38-
`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.
38+
`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.
3939

4040
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.
4141

examples/with-redux/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"next": "^2.0.0-beta",
11-
"next-connect-redux": "^0.1.1",
11+
"next-redux-wrapper": "^1.0.0",
1212
"react": "^15.4.2",
1313
"react-dom": "^15.4.2",
1414
"react-redux": "^5.0.1",

examples/with-redux/pages/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react'
2-
import { nextConnect, reducer, startClock, setPageTitle } from '../store'
2+
import { reducer, initStore, startClock } from '../store'
3+
import withRedux from 'next-redux-wrapper';
34
import Page from '../components/Page'
45

56
class Counter extends React.Component {
67
static getInitialProps ({ store, isServer }) {
78
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
8-
store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Index Page' })
99
return { isServer }
1010
}
1111

@@ -19,9 +19,9 @@ class Counter extends React.Component {
1919

2020
render () {
2121
return (
22-
<Page title={this.props.title} linkTo='/other' />
22+
<Page title='Index Page' linkTo='/other' />
2323
)
2424
}
2525
}
2626

27-
export default nextConnect((state) => state)(Counter)
27+
export default withRedux(initStore)(Counter)

examples/with-redux/pages/other.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react'
2-
import { nextConnect, reducer, startClock, setPageTitle } from '../store'
2+
import { reducer, initStore, startClock } from '../store'
3+
import withRedux from 'next-redux-wrapper';
34
import Page from '../components/Page'
45

56
class Counter extends React.Component {
67
static getInitialProps ({ store, isServer }) {
78
store.dispatch({ type: 'TICK', light: !isServer, ts: Date.now() })
8-
store.dispatch({ type: 'SET_PAGE_TITLE', title: 'Other Page' })
99
return { isServer }
1010
}
1111

@@ -19,9 +19,9 @@ class Counter extends React.Component {
1919

2020
render () {
2121
return (
22-
<Page title={this.props.title} linkTo='/' />
22+
<Page title='Other Page' linkTo='/' />
2323
)
2424
}
2525
}
2626

27-
export default nextConnect((state) => state)(Counter)
27+
export default withRedux(initStore)(Counter)

examples/with-redux/store.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
import { createStore, applyMiddleware } from 'redux'
22
import thunkMiddleware from 'redux-thunk'
3-
import nextConnectRedux from 'next-connect-redux'
43

5-
export const reducer = (state = { lastUpdate: 0, light: false, title: '' }, action) => {
4+
export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
65
switch (action.type) {
7-
case 'TICK': return { lastUpdate: action.ts, light: !!action.light, title: state.title }
8-
case 'SET_PAGE_TITLE': return { ...state, title: action.title }
6+
case 'TICK': return { lastUpdate: action.ts, light: !!action.light }
97
default: return state
108
}
119
}
1210

13-
export const setPageTitle = (title) => {
14-
return {
15-
type: 'SET_PAGE_TITLE',
16-
title
17-
}
18-
}
19-
2011
export const startClock = () => dispatch => {
2112
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
2213
}
2314

2415
export const initStore = (initialState) => {
2516
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
2617
}
27-
28-
export const nextConnect = nextConnectRedux(initStore)

0 commit comments

Comments
 (0)