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

Update to examples: with-redux (updated) (#1813)

* With-Redux-example-update-request

Hello Next.js,

I’ve added an additional example to “With-Redux” and updated some of
the original code to help illustrate to less inexperienced developers
how to implement Redux with Next.js.

The example is a simple counter to help reinforce how the client and
server renderings work together. In addition I also updated some of the
redux boilerplate code to help fully demonstrate how redux can be
implemented when using is with Next.js

Please contact me at spencer.bigum@gmail.com for further questions or
anything else you might need.

Thanks,
Spencer

* fixed listing issues: examples/with-redux

* Updated code based on @impronunciable Feedback
This commit is contained in:
spencer 2017-04-30 16:44:24 -04:00 committed by Dan Zajdband
parent 8fb6a03e48
commit 2e7bc1074d
6 changed files with 98 additions and 16 deletions

View file

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

View file

@ -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 (
<div>
<style jsx>{`
div {
padding: 0 0 20px 0;
}
`}</style>
<h1>AddCount: <span>{count}</span></h1>
<button onClick={this.add}>Add To Count</button>
</div>
)
}
}
const mapStateToProps = ({ count }) => ({ count })
const mapDispatchToProps = (dispatch) => {
return {
addCount: bindActionCreators(addCount, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(AddCount)

View file

@ -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 (
<div>
<h1>{title}</h1>
<Clock lastUpdate={lastUpdate} light={light} />
<AddCount />
<nav>
<Link href={linkTo}><a>Navigate</a></Link>
</nav>

View file

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

View file

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

View file

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