Skip to content

Commit 58bbf4e

Browse files
committed
Update docs with useActions hook removal
1 parent e4c0d4d commit 58bbf4e

File tree

2 files changed

+25
-59
lines changed

2 files changed

+25
-59
lines changed

docs/api/hooks.md

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ React Redux now offers a set of hook APIs as an alternative to the existing `con
1313

1414
> **Note**: The hook APIs listed in this page are **still experimental and in alpha!** We encourage you to try them out in your applications and give feedback, but be aware that they may be changed before a final release, including potential renaming or removal.
1515
16-
This page reflects the latest alpha, which is currently **v7.1.0-alpha.3**.
16+
These hooks were first added in v7.1.0.
17+
18+
This page reflects the latest alpha, which is currently **v7.1.0-alpha.4**.
1719

1820
## Using Hooks in a React Redux App
1921

@@ -80,68 +82,33 @@ export const TodoListItem = props => {
8082
}
8183
```
8284

83-
## `useActions()`
84-
85-
```js
86-
const boundAC = useActions(actionCreator : Function, deps : any[])
87-
88-
const boundACsObject = useActions(actionCreators : Object<string, Function>, deps : any[])
89-
90-
const boundACsArray = useActions(actionCreators : Function[], deps : any[])
91-
```
92-
93-
Allows you to prepare bound action creators that will dispatch actions to the Redux store when called.
94-
95-
This is conceptually similar to the [`mapDispatchToProps` argument to `connect`](../using-react-redux/connect-dispatching-actions-with-mapDispatchToProps.md). The action creators that are passed in will be bound using the Redux [`bindActionCreators()` utility](https://redux.js.org/api/bindactioncreators), and the bound functions will be returned.
96-
97-
However, there are some differences between the arguments passed to `useActions()` and the `mapDispatch` argument to `connect()`:
98-
99-
- `mapDispatch` may be either a function or an object. `useActions()` accepts a single action creator, an object full of action creators, or an array of action creators, and the return value will be the same form.
100-
- `mapDispatch` is normally used once when the component is instantiated, unless it is a function with the `(dispatch, ownProps)` signature, which causes it to be called any time the props have changed. The action creators passed to `useActions()` will be re-bound (and thus have new function references) whenever the values passed in the `deps` array change. If no `deps` array is provided, the functions will be re-bound every time the component re-renders.
101-
102-
> **Note**: There are potential edge cases with using the object argument form and declaring the object inline. See the [Usage Warnings](#usage-warnings) section of this page for further details.
103-
104-
You may call `useActions()` multiple times in a single component.
105-
106-
#### Examples
107-
108-
```jsx
109-
import React from 'react'
110-
import { useActions } from 'react-redux'
85+
## Removed: `useActions()`
11186

112-
const increaseCounter = amount => ({
113-
type: 'increase-counter',
114-
amount
115-
})
87+
This hook was removed in `v7.1.0-alpha.4`, based on [Dan Abramov's suggestion](https://github.com/reduxjs/react-redux/issues/1252#issuecomment-488160930).
88+
That suggestion was based on "binding action creators" not being as useful in a hooks-based use case, and causing too
89+
much conceptual overhead and syntactic complexity.
11690

117-
export const CounterComponent = ({ value }) => {
118-
// supports passing an object of action creators
119-
const { increaseCounterByOne, increaseCounterByTwo } = useActions(
120-
{
121-
increaseCounterByOne: () => increaseCounter(1),
122-
increaseCounterByTwo: () => increaseCounter(2)
123-
},
124-
[]
125-
)
91+
Instead, you should call the [`useDispatch`](#usedispatch) hook in your components to retrieve a reference to `dispatch`,
92+
and manually call `dispatch(someActionCreator())` in callbacks and effects as needed. You may also use the Redux
93+
[`bindActionCreators`](https://redux.js.org/api/bindactioncreators) function in your own code to bind action creators,
94+
or "manually" bind them like `const boundAddTodo = (text) => dispatch(addTodo(text))`.
12695

127-
// supports passing an array/tuple of action creators
128-
const [increaseCounterByThree, increaseCounterByFour] = useActions(
129-
[() => increaseCounter(3), () => increaseCounter(4)],
130-
[]
131-
)
96+
If you still wish to use the `useActions()` hook, you may copy and paste this implementation into your own app:
13297

133-
// supports passing a single action creator
134-
const increaseCounterBy5 = useActions(() => increaseCounter(5), [])
98+
```js
99+
import { bindActionCreators } from 'redux'
100+
import { useDispatch } from 'react-redux'
101+
import { useMemo } from 'react'
135102

136-
// passes through any arguments to the callback
137-
const increaseCounterByX = useActions(x => increaseCounter(x), [])
103+
export function useActions(actions, deps) {
104+
const dispatch = useDispatch()
105+
return useMemo(() => {
106+
if (Array.isArray(actions)) {
107+
return actions.map(a => bindActionCreators(a, dispatch))
108+
}
138109

139-
return (
140-
<div>
141-
<span>{value}</span>
142-
<button onClick={increaseCounterByOne}>Increase counter by 1</button>
143-
</div>
144-
)
110+
return bindActionCreators(actions, dispatch)
111+
}, deps)
145112
}
146113
```
147114

@@ -151,7 +118,6 @@ This hook was removed in `v7.1.0-alpha.3`, on the grounds that it didn't provide
151118

152119
If you were using it in your own code, please replace that with separate calls to `useSelector()` and `useActions()`.
153120

154-
155121
## `useDispatch()`
156122

157123
```js

website/siteConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const siteConfig = {
108108
* After that, 7.x will no longer appear in "pre-release" versions and we should remove this line
109109
* More info: https://docusaurus.io/docs/en/versioning
110110
*/
111-
nextVersion: '7.1.0-alpha.3',
111+
nextVersion: '7.1.0-alpha.4',
112112

113113
gaTrackingId: 'UA-130598673-2'
114114
}

0 commit comments

Comments
 (0)