1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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

This reverts commit ba54c6ac3d.
This commit is contained in:
Guillermo Rauch 2017-02-18 14:11:54 -03:00
parent ba54c6ac3d
commit 47e5231cdd
5 changed files with 14 additions and 25 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)
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.

View file

@ -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",

View file

@ -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 (
<Page title={this.props.title} linkTo='/other' />
<Page title='Index Page' linkTo='/other' />
)
}
}
export default nextConnect((state) => state)(Counter)
export default withRedux(initStore)(Counter)

View file

@ -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 (
<Page title={this.props.title} linkTo='/' />
<Page title='Other Page' linkTo='/' />
)
}
}
export default nextConnect((state) => state)(Counter)
export default withRedux(initStore)(Counter)

View file

@ -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)