Skip to content

Commit 0c7957e

Browse files
authored
Merge pull request #1 from reduxjs/master
Merge downstream
2 parents 33a2934 + 3745f0c commit 0c7957e

File tree

4 files changed

+2790
-2882
lines changed

4 files changed

+2790
-2882
lines changed

docs/api/applyMiddleware.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,17 @@ store
173173
// I can also dispatch a thunk async action from a component
174174
// any time its props change to load the missing data.
175175

176+
import React from 'react';
176177
import { connect } from 'react-redux'
177-
import { Component } from 'react'
178178

179-
class SandwichShop extends Component {
180-
componentDidMount() {
181-
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
182-
}
179+
function SandwichShop(props) {
180+
const { dispatch, forPerson } = props;
183181

184-
componentDidUpdate(prevProps) {
185-
if (prevProps.forPerson !== this.props.forPerson) {
186-
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
187-
}
188-
}
182+
useEffect(() => {
183+
dispatch(makeASandwichWithSecretSauce(forPerson));
184+
}, [forPerson]);
189185

190-
render() {
191-
return <p>{this.props.sandwiches.join('mustard')}</p>
192-
}
186+
return <p>{this.props.sandwiches.join('mustard')}</p>
193187
}
194188

195189
export default connect(state => ({

docs/api/bindActionCreators.md

+21-32
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function removeTodo(id) {
5050
#### `SomeComponent.js`
5151

5252
```js
53-
import { Component } from 'react'
53+
import React from 'react'
5454
import { bindActionCreators } from 'redux'
5555
import { 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

112101
export default connect(state => ({ todos: state.todos }))(TodoListContainer)

0 commit comments

Comments
 (0)