Skip to content

Commit

Permalink
Merge pull request #1 from reduxjs/master
Browse files Browse the repository at this point in the history
Merge downstream
  • Loading branch information
mxweaver authored Jan 9, 2022
2 parents 33a2934 + 3745f0c commit 0c7957e
Show file tree
Hide file tree
Showing 4 changed files with 2,790 additions and 2,882 deletions.
20 changes: 7 additions & 13 deletions docs/api/applyMiddleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,17 @@ store
// I can also dispatch a thunk async action from a component
// any time its props change to load the missing data.

import React from 'react';
import { connect } from 'react-redux'
import { Component } from 'react'

class SandwichShop extends Component {
componentDidMount() {
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
}
function SandwichShop(props) {
const { dispatch, forPerson } = props;

componentDidUpdate(prevProps) {
if (prevProps.forPerson !== this.props.forPerson) {
this.props.dispatch(makeASandwichWithSecretSauce(this.props.forPerson))
}
}
useEffect(() => {
dispatch(makeASandwichWithSecretSauce(forPerson));
}, [forPerson]);

render() {
return <p>{this.props.sandwiches.join('mustard')}</p>
}
return <p>{this.props.sandwiches.join('mustard')}</p>
}

export default connect(state => ({
Expand Down
53 changes: 21 additions & 32 deletions docs/api/bindActionCreators.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function removeTodo(id) {
#### `SomeComponent.js`

```js
import { Component } from 'react'
import React from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

Expand All @@ -61,29 +61,23 @@ console.log(TodoActionCreators)
// removeTodo: Function
// }

class TodoListContainer extends Component {
constructor(props) {
super(props)
function TodoListContainer(props) {
// Injected by react-redux:
const { dispatch, todos } = props

const { dispatch } = props
// Here's a good use case for bindActionCreators:
// You want a child component to be completely unaware of Redux.
// We create bound versions of these functions now so we can
// pass them down to our child later.

// Here's a good use case for bindActionCreators:
// You want a child component to be completely unaware of Redux.
// We create bound versions of these functions now so we can
// pass them down to our child later.

this.boundActionCreators = bindActionCreators(TodoActionCreators, dispatch)
console.log(this.boundActionCreators)
// {
// addTodo: Function,
// removeTodo: Function
// }
}

componentDidMount() {
// Injected by react-redux:
let { dispatch } = this.props
const boundActionCreators = useMemo(() => bindActionCreators(TodoActionCreators, dispatch), [dispatch]);
console.log(boundActionCreators)
// {
// addTodo: Function,
// removeTodo: Function
// }

useEffect(() => {
// Note: this won't work:
// TodoActionCreators.addTodo('Use Redux')

Expand All @@ -93,20 +87,15 @@ class TodoListContainer extends Component {
// This will work:
let action = TodoActionCreators.addTodo('Use Redux')
dispatch(action)
}

render() {
// Injected by react-redux:
let { todos } = this.props
}, []);

return <TodoList todos={todos} {...this.boundActionCreators} />
return <TodoList todos={todos} {...this.boundActionCreators} />

// An alternative to bindActionCreators is to pass
// just the dispatch function down, but then your child component
// needs to import action creators and know about them.
// An alternative to bindActionCreators is to pass
// just the dispatch function down, but then your child component
// needs to import action creators and know about them.

// return <TodoList todos={todos} dispatch={dispatch} />
}
// return <TodoList todos={todos} dispatch={dispatch} />
}

export default connect(state => ({ todos: state.todos }))(TodoListContainer)
Expand Down
Loading

0 comments on commit 0c7957e

Please sign in to comment.