Skip to content

Commit 3e91232

Browse files
NaturalclarLuis Fernando Alvarez D
authored and
Luis Fernando Alvarez D
committed
Update redux example using hooks api (vercel#8410)
* Update redux example with hooks api * Update react and react-redux package * Apply standard style
1 parent a256270 commit 3e91232

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

examples/with-redux/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ In the first example we are going to display a digital clock that updates every
4747

4848
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.
4949

50-
All pages have access to the redux store using `connect` from `react-redux`.
50+
`index.js` have access to the redux store using `connect` from `react-redux`.
51+
`counter.js` and `examples.js` have access to the redux store using `useSelector` and `useDispatch` from `react-redux@^7.1.0`
5152

5253
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.
5354

+27-37
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
1-
import React, { Component } from 'react'
2-
import { connect } from 'react-redux'
3-
import { bindActionCreators } from 'redux'
1+
import React from 'react'
2+
import { useSelector, useDispatch } from 'react-redux'
43
import { incrementCount, decrementCount, resetCount } from '../store'
54

6-
class Counter extends Component {
7-
increment = () => {
8-
const { incrementCount } = this.props
9-
incrementCount()
10-
}
5+
const countSelector = state => state.count
116

12-
decrement = () => {
13-
const { decrementCount } = this.props
14-
decrementCount()
7+
const useCounter = () => {
8+
const dispatch = useDispatch()
9+
const increment = () => {
10+
dispatch(incrementCount())
1511
}
16-
17-
reset = () => {
18-
const { resetCount } = this.props
19-
resetCount()
12+
const decrement = () => {
13+
dispatch(decrementCount())
2014
}
21-
22-
render () {
23-
const { count } = this.props
24-
return (
25-
<div>
26-
<h1>
27-
Count: <span>{count}</span>
28-
</h1>
29-
<button onClick={this.increment}>+1</button>
30-
<button onClick={this.decrement}>-1</button>
31-
<button onClick={this.reset}>Reset</button>
32-
</div>
33-
)
15+
const reset = () => {
16+
dispatch(resetCount())
3417
}
18+
19+
return { increment, decrement, reset }
3520
}
3621

37-
function mapStateToProps (state) {
38-
const { count } = state
39-
return { count }
22+
function Counter () {
23+
const count = useSelector(countSelector)
24+
const { increment, decrement, reset } = useCounter()
25+
return (
26+
<div>
27+
<h1>
28+
Count: <span>{count}</span>
29+
</h1>
30+
<button onClick={increment}>+1</button>
31+
<button onClick={decrement}>-1</button>
32+
<button onClick={reset}>Reset</button>
33+
</div>
34+
)
4035
}
41-
const mapDispatchToProps = dispatch =>
42-
bindActionCreators({ incrementCount, decrementCount, resetCount }, dispatch)
4336

44-
export default connect(
45-
mapStateToProps,
46-
mapDispatchToProps
47-
)(Counter)
37+
export default Counter
+9-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { connect } from 'react-redux'
1+
import { useSelector, shallowEqual } from 'react-redux'
22
import Clock from './clock'
33
import Counter from './counter'
44

5-
function Examples ({ lastUpdate, light }) {
5+
const clockSelector = state => ({
6+
lastUpdate: state.lastUpdate,
7+
light: state.light
8+
})
9+
10+
function Examples () {
11+
const { lastUpdate, light } = useSelector(clockSelector, shallowEqual)
612
return (
713
<div>
814
<Clock lastUpdate={lastUpdate} light={light} />
@@ -11,9 +17,4 @@ function Examples ({ lastUpdate, light }) {
1117
)
1218
}
1319

14-
function mapStateToProps (state) {
15-
const { lastUpdate, light } = state
16-
return { lastUpdate, light }
17-
}
18-
19-
export default connect(mapStateToProps)(Examples)
20+
export default Examples

examples/with-redux/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
},
99
"dependencies": {
1010
"next": "latest",
11-
"react": "^16.7.0",
11+
"react": "^16.9.0",
1212
"redux-devtools-extension": "^2.13.2",
13-
"react-dom": "^16.7.0",
14-
"react-redux": "^5.0.1",
13+
"react-dom": "^16.9.0",
14+
"react-redux": "^7.1.0",
1515
"redux": "^3.6.0"
1616
},
1717
"license": "ISC"

0 commit comments

Comments
 (0)