Commit 3e912323 authored by Jesse Katsumata's avatar Jesse Katsumata Committed by Luis Fernando Alvarez D

Update redux example using hooks api (#8410)

* Update redux example with hooks api

* Update react and react-redux package

* Apply standard style
parent a256270e
...@@ -47,7 +47,8 @@ In the first example we are going to display a digital clock that updates every ...@@ -47,7 +47,8 @@ In the first example we are going to display a digital clock that updates every
The Redux `Provider` is implemented in `pages/_app.js`. Since the `MyApp` component is wrapped in `withReduxStore` the redux store will be automatically initialized and provided to `MyApp`, which in turn passes it off to `react-redux`'s `Provider` component. The Redux `Provider` is implemented in `pages/_app.js`. Since the `MyApp` component is wrapped in `withReduxStore` the redux store will be automatically initialized and provided to `MyApp`, which in turn passes it off to `react-redux`'s `Provider` component.
All pages have access to the redux store using `connect` from `react-redux`. `index.js` have access to the redux store using `connect` from `react-redux`.
`counter.js` and `examples.js` have access to the redux store using `useSelector` and `useDispatch` from `react-redux@^7.1.0`
On the server side every request initializes a new store, because otherwise different user data can be mixed up. On the client side the same store is used, even between page changes. On the server side every request initializes a new store, because otherwise different user data can be mixed up. On the client side the same store is used, even between page changes.
......
import React, { Component } from 'react' import React from 'react'
import { connect } from 'react-redux' import { useSelector, useDispatch } from 'react-redux'
import { bindActionCreators } from 'redux'
import { incrementCount, decrementCount, resetCount } from '../store' import { incrementCount, decrementCount, resetCount } from '../store'
class Counter extends Component { const countSelector = state => state.count
increment = () => {
const { incrementCount } = this.props
incrementCount()
}
decrement = () => { const useCounter = () => {
const { decrementCount } = this.props const dispatch = useDispatch()
decrementCount() const increment = () => {
dispatch(incrementCount())
} }
const decrement = () => {
reset = () => { dispatch(decrementCount())
const { resetCount } = this.props
resetCount()
} }
const reset = () => {
render () { dispatch(resetCount())
const { count } = this.props
return (
<div>
<h1>
Count: <span>{count}</span>
</h1>
<button onClick={this.increment}>+1</button>
<button onClick={this.decrement}>-1</button>
<button onClick={this.reset}>Reset</button>
</div>
)
} }
return { increment, decrement, reset }
} }
function mapStateToProps (state) { function Counter () {
const { count } = state const count = useSelector(countSelector)
return { count } const { increment, decrement, reset } = useCounter()
return (
<div>
<h1>
Count: <span>{count}</span>
</h1>
<button onClick={increment}>+1</button>
<button onClick={decrement}>-1</button>
<button onClick={reset}>Reset</button>
</div>
)
} }
const mapDispatchToProps = dispatch =>
bindActionCreators({ incrementCount, decrementCount, resetCount }, dispatch)
export default connect( export default Counter
mapStateToProps,
mapDispatchToProps
)(Counter)
import { connect } from 'react-redux' import { useSelector, shallowEqual } from 'react-redux'
import Clock from './clock' import Clock from './clock'
import Counter from './counter' import Counter from './counter'
function Examples ({ lastUpdate, light }) { const clockSelector = state => ({
lastUpdate: state.lastUpdate,
light: state.light
})
function Examples () {
const { lastUpdate, light } = useSelector(clockSelector, shallowEqual)
return ( return (
<div> <div>
<Clock lastUpdate={lastUpdate} light={light} /> <Clock lastUpdate={lastUpdate} light={light} />
...@@ -11,9 +17,4 @@ function Examples ({ lastUpdate, light }) { ...@@ -11,9 +17,4 @@ function Examples ({ lastUpdate, light }) {
) )
} }
function mapStateToProps (state) { export default Examples
const { lastUpdate, light } = state
return { lastUpdate, light }
}
export default connect(mapStateToProps)(Examples)
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.9.0",
"redux-devtools-extension": "^2.13.2", "redux-devtools-extension": "^2.13.2",
"react-dom": "^16.7.0", "react-dom": "^16.9.0",
"react-redux": "^5.0.1", "react-redux": "^7.1.0",
"redux": "^3.6.0" "redux": "^3.6.0"
}, },
"license": "ISC" "license": "ISC"
......
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