Skip to content

Commit b0be6d2

Browse files
authored
Stop using bindActionCreators inside of Render
The example was calling `bindActionCreators` inside of `render()`, which would produce new functions on every re-render. Moved that to the constructor so that they're only done once.
1 parent ab5c959 commit b0be6d2

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

docs/api/bindActionCreators.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ console.log(TodoActionCreators)
5353
// }
5454

5555
class TodoListContainer extends Component {
56+
constructor(props) {
57+
super(props);
58+
59+
const {dispatch} = props;
60+
61+
// Here's a good use case for bindActionCreators:
62+
// You want a child component to be completely unaware of Redux.
63+
// We create bound versions of these functions now so we can
64+
// pass them down to our child later.
65+
66+
this.boundActionCreators = bindActionCreators(TodoActionCreators, dispatch)
67+
console.log(this.boundActionCreators)
68+
// {
69+
// addTodo: Function,
70+
// removeTodo: Function
71+
// }
72+
}
73+
5674
componentDidMount() {
5775
// Injected by react-redux:
5876
let { dispatch } = this.props
@@ -70,19 +88,9 @@ class TodoListContainer extends Component {
7088

7189
render() {
7290
// Injected by react-redux:
73-
let { todos, dispatch } = this.props
74-
75-
// Here's a good use case for bindActionCreators:
76-
// You want a child component to be completely unaware of Redux.
77-
78-
let boundActionCreators = bindActionCreators(TodoActionCreators, dispatch)
79-
console.log(boundActionCreators)
80-
// {
81-
// addTodo: Function,
82-
// removeTodo: Function
83-
// }
91+
let { todos } = this.props
8492

85-
return <TodoList todos={todos} {...boundActionCreators} />
93+
return <TodoList todos={todos} {...this.boundActionCreators} />
8694

8795
// An alternative to bindActionCreators is to pass
8896
// just the dispatch function down, but then your child component

0 commit comments

Comments
 (0)