@@ -50,7 +50,7 @@ export function removeTodo(id) {
50
50
#### ` SomeComponent.js `
51
51
52
52
``` js
53
- import { Component } from ' react'
53
+ import React from ' react'
54
54
import { bindActionCreators } from ' redux'
55
55
import { connect } from ' react-redux'
56
56
@@ -61,29 +61,23 @@ console.log(TodoActionCreators)
61
61
// removeTodo: Function
62
62
// }
63
63
64
- class TodoListContainer extends Component {
65
- constructor ( props ) {
66
- super ( props)
64
+ function TodoListContainer ( props ) {
65
+ // Injected by react-redux:
66
+ const { dispatch , todos } = props
67
67
68
- const { dispatch } = props
68
+ // Here's a good use case for bindActionCreators:
69
+ // You want a child component to be completely unaware of Redux.
70
+ // We create bound versions of these functions now so we can
71
+ // pass them down to our child later.
69
72
70
- // Here's a good use case for bindActionCreators:
71
- // You want a child component to be completely unaware of Redux.
72
- // We create bound versions of these functions now so we can
73
- // pass them down to our child later.
74
-
75
- this .boundActionCreators = bindActionCreators (TodoActionCreators, dispatch)
76
- console .log (this .boundActionCreators )
77
- // {
78
- // addTodo: Function,
79
- // removeTodo: Function
80
- // }
81
- }
82
-
83
- componentDidMount () {
84
- // Injected by react-redux:
85
- let { dispatch } = this .props
73
+ const boundActionCreators = useMemo (() => bindActionCreators (TodoActionCreators, dispatch), [dispatch]);
74
+ console .log (boundActionCreators)
75
+ // {
76
+ // addTodo: Function,
77
+ // removeTodo: Function
78
+ // }
86
79
80
+ useEffect (() => {
87
81
// Note: this won't work:
88
82
// TodoActionCreators.addTodo('Use Redux')
89
83
@@ -93,20 +87,15 @@ class TodoListContainer extends Component {
93
87
// This will work:
94
88
let action = TodoActionCreators .addTodo (' Use Redux' )
95
89
dispatch (action)
96
- }
97
-
98
- render () {
99
- // Injected by react-redux:
100
- let { todos } = this .props
90
+ }, []);
101
91
102
- return < TodoList todos= {todos} {... this .boundActionCreators } / >
92
+ return < TodoList todos= {todos} {... this .boundActionCreators } / >
103
93
104
- // An alternative to bindActionCreators is to pass
105
- // just the dispatch function down, but then your child component
106
- // needs to import action creators and know about them.
94
+ // An alternative to bindActionCreators is to pass
95
+ // just the dispatch function down, but then your child component
96
+ // needs to import action creators and know about them.
107
97
108
- // return <TodoList todos={todos} dispatch={dispatch} />
109
- }
98
+ // return <TodoList todos={todos} dispatch={dispatch} />
110
99
}
111
100
112
101
export default connect (state => ({ todos: state .todos }))(TodoListContainer)
0 commit comments