Skip to content

Commit ab95c6d

Browse files
matamatanotLuis Fernando Alvarez D
authored and
Luis Fernando Alvarez D
committed
Updated with-redux-thunk example using useSelector and useDispatch (vercel#8396)
* use useSelector and useDispatch * update README * fix space and trailing comma
1 parent 308c2cd commit ab95c6d

File tree

5 files changed

+35
-60
lines changed

5 files changed

+35
-60
lines changed

examples/with-redux-thunk/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ 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

54-
The example under `components/counter.js`, shows a simple incremental counter implementing a common Redux pattern of mapping state to 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 when switching pages on the client side
55+
The example under `components/counter.js`, shows a simple incremental counter implementing a common Redux pattern. 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 when switching pages on the client side
5556

5657
For simplicity and readability, Reducers, Actions, and Store creators are all in the same file: `store.js`
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
1-
import React, { Component } from 'react'
2-
import { connect } from 'react-redux'
1+
import React from 'react'
2+
import { useSelector, useDispatch } from 'react-redux'
33
import { incrementCount, decrementCount, resetCount } from '../store'
44

5-
class Counter extends Component {
6-
increment = () => {
7-
const { dispatch } = this.props
8-
dispatch(incrementCount())
9-
}
5+
export default () => {
6+
const count = useSelector(state => state.count)
7+
const dispatch = useDispatch()
108

11-
decrement = () => {
12-
const { dispatch } = this.props
13-
dispatch(decrementCount())
14-
}
15-
16-
reset = () => {
17-
const { dispatch } = this.props
18-
dispatch(resetCount())
19-
}
20-
21-
render () {
22-
const { count } = this.props
23-
return (
24-
<div>
25-
<h1>
26-
Count: <span>{count}</span>
27-
</h1>
28-
<button onClick={this.increment}>+1</button>
29-
<button onClick={this.decrement}>-1</button>
30-
<button onClick={this.reset}>Reset</button>
31-
</div>
32-
)
33-
}
9+
return (
10+
<div>
11+
<h1>
12+
Count: <span>{count}</span>
13+
</h1>
14+
<button onClick={() => dispatch(incrementCount())}>+1</button>
15+
<button onClick={() => dispatch(decrementCount())}>-1</button>
16+
<button onClick={() => dispatch(resetCount())}>Reset</button>
17+
</div>
18+
)
3419
}
35-
36-
function mapStateToProps (state) {
37-
const { count } = state
38-
return { count }
39-
}
40-
41-
export default connect(mapStateToProps)(Counter)
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import { connect } from 'react-redux'
1+
import { useSelector } from 'react-redux'
22
import Clock from './clock'
33
import Counter from './counter'
44

5-
function Examples ({ lastUpdate, light }) {
5+
export default () => {
6+
const lastUpdate = useSelector(state => state.lastUpdate)
7+
const light = useSelector(state => state.light)
8+
69
return (
710
<div>
811
<Clock lastUpdate={lastUpdate} light={light} />
912
<Counter />
1013
</div>
1114
)
1215
}
13-
14-
function mapStateToProps (state) {
15-
const { lastUpdate, light } = state
16-
return { lastUpdate, light }
17-
}
18-
19-
export default connect(mapStateToProps)(Examples)

examples/with-redux-thunk/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
},
99
"dependencies": {
1010
"next": "latest",
11-
"react": "^16.7.0",
12-
"redux-devtools-extension": "^2.13.2",
13-
"react-dom": "^16.7.0",
14-
"react-redux": "^5.0.1",
15-
"redux": "^3.6.0",
16-
"redux-thunk": "^2.1.0"
11+
"react": "^16.9.0",
12+
"redux-devtools-extension": "^2.13.8",
13+
"react-dom": "^16.9.0",
14+
"react-redux": "^7.1.0",
15+
"redux": "^4.0.4",
16+
"redux-thunk": "^2.3.0"
1717
},
1818
"license": "ISC"
1919
}

examples/with-redux-thunk/store.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ export const startClock = dispatch => {
5151
}, 1000)
5252
}
5353

54-
export const incrementCount = () => dispatch => {
55-
return dispatch({ type: actionTypes.INCREMENT })
54+
export const incrementCount = () => {
55+
return { type: actionTypes.INCREMENT }
5656
}
5757

58-
export const decrementCount = () => dispatch => {
59-
return dispatch({ type: actionTypes.DECREMENT })
58+
export const decrementCount = () => {
59+
return { type: actionTypes.DECREMENT }
6060
}
6161

62-
export const resetCount = () => dispatch => {
63-
return dispatch({ type: actionTypes.RESET })
62+
export const resetCount = () => {
63+
return { type: actionTypes.RESET }
6464
}
6565

6666
export function initializeStore (initialState = exampleInitialState) {

0 commit comments

Comments
 (0)