Commit 3db3f4b4 authored by yhirano55's avatar yhirano55 Committed by Tim Neutkens

Improve with-redux example (#4377)

It's better that Counter behave not only `increment (add)` but also `decrement` or `reset`.

* Add decrement, reset handlers and rename add handler in `components/counter.js`
* Add increment, decrement, reset actions to `store.js`
* Rename page component class name: Counter => Index in `pages/index.js`
* Remove needless dispatch count event on getInitialProps in `pages/index.js`
* Format JSX to be readable in `pages/_app.js`
* Remove needless line from `components/examples.js`
* Remove needless spaces in `lib/with-redux-store.js`
parent 2c0d6002
import React, {Component} from 'react' import React, {Component} from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { addCount } from '../store' import { incrementCount, decrementCount, resetCount } from '../store'
class AddCount extends Component { class Counter extends Component {
add = () => { increment = () => {
const {dispatch} = this.props const {dispatch} = this.props
dispatch(addCount()) dispatch(incrementCount())
}
decrement = () => {
const {dispatch} = this.props
dispatch(decrementCount())
}
reset = () => {
const {dispatch} = this.props
dispatch(resetCount())
} }
render () { render () {
const { count } = this.props const { count } = this.props
return ( return (
<div> <div>
<h1>AddCount: <span>{count}</span></h1> <h1>Count: <span>{count}</span></h1>
<button onClick={this.add}>Add To Count</button> <button onClick={this.increment}>+1</button>
<button onClick={this.decrement}>-1</button>
<button onClick={this.reset}>Reset</button>
</div> </div>
) )
} }
...@@ -24,4 +36,4 @@ function mapStateToProps (state) { ...@@ -24,4 +36,4 @@ function mapStateToProps (state) {
return {count} return {count}
} }
export default connect(mapStateToProps)(AddCount) export default connect(mapStateToProps)(Counter)
import {connect} from 'react-redux' import {connect} from 'react-redux'
import Clock from './clock' import Clock from './clock'
import Counter from './counter' import Counter from './counter'
......
...@@ -22,7 +22,7 @@ export default (App) => { ...@@ -22,7 +22,7 @@ export default (App) => {
return class Redux extends React.Component { return class Redux extends React.Component {
static async getInitialProps (appContext) { static async getInitialProps (appContext) {
const reduxStore = getOrCreateStore() const reduxStore = getOrCreateStore()
// Provide the store to getInitialProps of pages // Provide the store to getInitialProps of pages
appContext.ctx.reduxStore = reduxStore appContext.ctx.reduxStore = reduxStore
...@@ -46,4 +46,4 @@ export default (App) => { ...@@ -46,4 +46,4 @@ export default (App) => {
return <App {...this.props} reduxStore={this.reduxStore} /> return <App {...this.props} reduxStore={this.reduxStore} />
} }
} }
} }
...@@ -6,11 +6,13 @@ import { Provider } from 'react-redux' ...@@ -6,11 +6,13 @@ import { Provider } from 'react-redux'
class MyApp extends App { class MyApp extends App {
render () { render () {
const {Component, pageProps, reduxStore} = this.props const {Component, pageProps, reduxStore} = this.props
return <Container> return (
<Provider store={reduxStore}> <Container>
<Component {...pageProps} /> <Provider store={reduxStore}>
</Provider> <Component {...pageProps} />
</Container> </Provider>
</Container>
)
} }
} }
......
import React from 'react' import React from 'react'
import {connect} from 'react-redux' import {connect} from 'react-redux'
import {startClock, addCount, serverRenderClock} from '../store' import {startClock, serverRenderClock} from '../store'
import Examples from '../components/examples' import Examples from '../components/examples'
class Counter extends React.Component { class Index extends React.Component {
static getInitialProps ({ reduxStore, req }) { static getInitialProps ({ reduxStore, req }) {
const isServer = !!req const isServer = !!req
reduxStore.dispatch(serverRenderClock(isServer)) reduxStore.dispatch(serverRenderClock(isServer))
reduxStore.dispatch(addCount())
return {} return {}
} }
...@@ -28,4 +27,4 @@ class Counter extends React.Component { ...@@ -28,4 +27,4 @@ class Counter extends React.Component {
} }
} }
export default connect()(Counter) export default connect()(Index)
...@@ -9,19 +9,32 @@ const exampleInitialState = { ...@@ -9,19 +9,32 @@ const exampleInitialState = {
} }
export const actionTypes = { export const actionTypes = {
ADD: 'ADD', TICK: 'TICK',
TICK: 'TICK' INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT',
RESET: 'RESET'
} }
// REDUCERS // REDUCERS
export const reducer = (state = exampleInitialState, action) => { export const reducer = (state = exampleInitialState, action) => {
switch (action.type) { switch (action.type) {
case actionTypes.TICK: case actionTypes.TICK:
return Object.assign({}, state, { lastUpdate: action.ts, light: !!action.light }) return Object.assign({}, state, {
case actionTypes.ADD: lastUpdate: action.ts,
light: !!action.light
})
case actionTypes.INCREMENT:
return Object.assign({}, state, { return Object.assign({}, state, {
count: state.count + 1 count: state.count + 1
}) })
case actionTypes.DECREMENT:
return Object.assign({}, state, {
count: state.count - 1
})
case actionTypes.RESET:
return Object.assign({}, state, {
count: exampleInitialState.count
})
default: return state default: return state
} }
} }
...@@ -38,8 +51,16 @@ export const startClock = dispatch => { ...@@ -38,8 +51,16 @@ export const startClock = dispatch => {
}, 1000) }, 1000)
} }
export const addCount = () => dispatch => { export const incrementCount = () => dispatch => {
return dispatch({ type: actionTypes.ADD }) return dispatch({ type: actionTypes.INCREMENT })
}
export const decrementCount = () => dispatch => {
return dispatch({ type: actionTypes.DECREMENT })
}
export const resetCount = () => dispatch => {
return dispatch({ type: actionTypes.RESET })
} }
export function initializeStore (initialState = exampleInitialState) { export function initializeStore (initialState = exampleInitialState) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment