@@ -50,7 +50,7 @@ export function removeTodo(id) {
5050#### ` SomeComponent.js `
5151
5252``` js
53- import { Component } from ' react'
53+ import React from ' react'
5454import { bindActionCreators } from ' redux'
5555import { connect } from ' react-redux'
5656
@@ -61,29 +61,23 @@ console.log(TodoActionCreators)
6161// removeTodo: Function
6262// }
6363
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
6767
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.
6972
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+ // }
8679
80+ useEffect (() => {
8781 // Note: this won't work:
8882 // TodoActionCreators.addTodo('Use Redux')
8983
@@ -93,20 +87,15 @@ class TodoListContainer extends Component {
9387 // This will work:
9488 let action = TodoActionCreators .addTodo (' Use Redux' )
9589 dispatch (action)
96- }
97-
98- render () {
99- // Injected by react-redux:
100- let { todos } = this .props
90+ }, []);
10191
102- return < TodoList todos= {todos} {... this .boundActionCreators } / >
92+ return < TodoList todos= {todos} {... this .boundActionCreators } / >
10393
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.
10797
108- // return <TodoList todos={todos} dispatch={dispatch} />
109- }
98+ // return <TodoList todos={todos} dispatch={dispatch} />
11099}
111100
112101export default connect (state => ({ todos: state .todos }))(TodoListContainer)
0 commit comments