diff --git a/examples/with-redux/README.md b/examples/with-redux/README.md index 6b7d6759..71645417 100644 --- a/examples/with-redux/README.md +++ b/examples/with-redux/README.md @@ -28,7 +28,7 @@ now Usually splitting your app state into `pages` feels natural but sometimes you'll want to have global state for your app. This is an example on how you can use redux that also works with our universal rendering approach. This is just a way you can do it but it's not the only one. -In this example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color than the client one. +In the first example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color than the client one. ![](http://i.imgur.com/JCxtWSj.gif) @@ -43,3 +43,7 @@ To pass the initial state from the server to the client we pass it as a prop cal The trick here for supporting universal redux is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store. That's what we accomplish on `store.js` The clock, under `components/Clock.js`, has access to the state using the `connect` function from `react-redux`. In this case Clock is a direct child from the page but it could be deep down the render tree. + +The second example, under `components/AddCount.js`, shows a simple add counter function with a class component implementing a common redux pattern of mapping state and props. Again, the first render is happening in the server and instead of starting the count at 0, it will dispatch an action in redux that starts the count at 1. This continues to highlight how each navigation triggers a server render first and then a client render second, when you navigate between pages. + +For simplicity and readability, Reducers, Actions, and Store creators are all in the same file: `store.js` diff --git a/examples/with-redux/components/AddCount.js b/examples/with-redux/components/AddCount.js new file mode 100644 index 00000000..2bf76f3a --- /dev/null +++ b/examples/with-redux/components/AddCount.js @@ -0,0 +1,35 @@ +import React, {Component} from 'react' +import { connect } from 'react-redux' +import { bindActionCreators } from 'redux' +import { addCount } from '../store' + +class AddCount extends Component { + add = () => { + this.props.addCount() + } + + render () { + const { count } = this.props + return ( +
+ +

AddCount: {count}

+ +
+ ) + } +} + +const mapStateToProps = ({ count }) => ({ count }) + +const mapDispatchToProps = (dispatch) => { + return { + addCount: bindActionCreators(addCount, dispatch) + } +} + +export default connect(mapStateToProps, mapDispatchToProps)(AddCount) diff --git a/examples/with-redux/components/Page.js b/examples/with-redux/components/Page.js index f7941894..3532e3fb 100644 --- a/examples/with-redux/components/Page.js +++ b/examples/with-redux/components/Page.js @@ -1,12 +1,14 @@ import Link from 'next/link' import { connect } from 'react-redux' import Clock from './Clock' +import AddCount from './AddCount' export default connect(state => state)(({ title, linkTo, lastUpdate, light }) => { return (

{title}

+ diff --git a/examples/with-redux/pages/index.js b/examples/with-redux/pages/index.js index 0663cfb7..be66747b 100644 --- a/examples/with-redux/pages/index.js +++ b/examples/with-redux/pages/index.js @@ -1,16 +1,19 @@ import React from 'react' -import { initStore, startClock } from '../store' +import { bindActionCreators } from 'redux' +import { initStore, startClock, addCount, serverRenderClock } 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(serverRenderClock(isServer)) + store.dispatch(addCount()) + return { isServer } } componentDidMount () { - this.timer = this.props.dispatch(startClock()) + this.timer = this.props.startClock() } componentWillUnmount () { @@ -24,4 +27,11 @@ class Counter extends React.Component { } } -export default withRedux(initStore)(Counter) +const mapDispatchToProps = (dispatch) => { + return { + addCount: bindActionCreators(addCount, dispatch), + startClock: bindActionCreators(startClock, dispatch) + } +} + +export default withRedux(initStore, null, mapDispatchToProps)(Counter) diff --git a/examples/with-redux/pages/other.js b/examples/with-redux/pages/other.js index 8152dd13..26bfa708 100644 --- a/examples/with-redux/pages/other.js +++ b/examples/with-redux/pages/other.js @@ -1,20 +1,18 @@ import React from 'react' -import { initStore, startClock } from '../store' +import { bindActionCreators } from 'redux' +import { initStore, startClock, addCount, serverRenderClock } 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(serverRenderClock(isServer)) + store.dispatch(addCount()) return { isServer } } componentDidMount () { - this.timer = this.props.dispatch(startClock()) - } - - componentWillUnmount () { - clearInterval(this.timer) + this.timer = this.props.startClock() } render () { @@ -24,4 +22,11 @@ class Counter extends React.Component { } } -export default withRedux(initStore)(Counter) +const mapDispatchToProps = (dispatch) => { + return { + addCount: bindActionCreators(addCount, dispatch), + startClock: bindActionCreators(startClock, dispatch) + } +} + +export default withRedux(initStore, null, mapDispatchToProps)(Counter) diff --git a/examples/with-redux/store.js b/examples/with-redux/store.js index a15a9f81..71ab2d5e 100644 --- a/examples/with-redux/store.js +++ b/examples/with-redux/store.js @@ -1,17 +1,43 @@ import { createStore, applyMiddleware } from 'redux' import thunkMiddleware from 'redux-thunk' -export const reducer = (state = { lastUpdate: 0, light: false }, action) => { +const exampleInitialState = { + lastUpdate: 0, + light: false, + count: 0 +} + +export const actionTypes = { + ADD: 'ADD', + TICK: 'TICK' +} + +// REDUCERS +export const reducer = (state = exampleInitialState, action) => { switch (action.type) { - case 'TICK': return { lastUpdate: action.ts, light: !!action.light } + case actionTypes.TICK: + return Object.assign({}, state, { lastUpdate: action.ts, light: !!action.light }) + case actionTypes.ADD: + return Object.assign({}, state, { + count: state.count + 1 + }) default: return state } } +// ACTIONS +export const serverRenderClock = (isServer) => dispatch => { + return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() }) +} + export const startClock = () => dispatch => { return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800) } -export const initStore = (initialState) => { +export const addCount = () => dispatch => { + return dispatch({ type: actionTypes.ADD }) +} + +export const initStore = (initialState = exampleInitialState) => { return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) }