Skip to content

Commit f2cfe29

Browse files
MrWolfZtimdorr
authored andcommitted
use proper example code tag in hooks JSDoc comments (#1257)
* use proper example code tag in hooks JSDoc comments * fix mistake in `useActions` hook example code * remove TypeScript annotations from example code and adjust `useReduxContext` hook to also use @example JSDoc tag
1 parent d06316d commit f2cfe29

File tree

6 files changed

+113
-119
lines changed

6 files changed

+113
-119
lines changed

src/hooks/useActions.js

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,57 @@ import { useMemo } from 'react'
66
/**
77
* A hook to bind action creators to the redux store's `dispatch` function
88
* similar to how redux's `bindActionCreators` works.
9-
*
9+
*
1010
* Supports passing a single action creator, an array/tuple of action
1111
* creators, or an object of action creators.
12-
*
12+
*
1313
* Any arguments passed to the created callbacks are passed through to
14-
* the your functions.
15-
*
14+
* your functions.
15+
*
1616
* This hook takes a dependencies array as an optional second argument,
1717
* which when passed ensures referential stability of the created callbacks.
18-
*
18+
*
1919
* @param {Function|Function[]|Object.<string, Function>} actions the action creators to bind
2020
* @param {any[]} deps (optional) dependencies array to control referential stability
21-
*
21+
*
2222
* @returns {Function|Function[]|Object.<string, Function>} callback(s) bound to store's `dispatch` function
2323
*
24-
* Usage:
24+
* @example
2525
*
26-
```jsx
27-
import React from 'react'
28-
import { useActions } from 'react-redux'
29-
30-
const increaseCounter = ({ amount }) => ({
31-
type: 'increase-counter',
32-
amount,
33-
})
34-
35-
export const CounterComponent = ({ value }) => {
36-
// supports passing an object of action creators
37-
const { increaseCounterByOne, increaseCounterByTwo } = useActions({
38-
increaseCounterByOne: () => increaseCounter(1),
39-
increaseCounterByTwo: () => increaseCounter(2),
40-
}, [])
41-
42-
// supports passing an array/tuple of action creators
43-
const [increaseCounterByThree, increaseCounterByFour] = useActions([
44-
() => increaseCounter(3),
45-
() => increaseCounter(4),
46-
], [])
47-
48-
// supports passing a single action creator
49-
const increaseCounterBy5 = useActions(() => increaseCounter(5), [])
50-
51-
// passes through any arguments to the callback
52-
const increaseCounterByX = useActions(x => increaseCounter(x), [])
53-
54-
return (
55-
<div>
56-
<span>{value}</span>
57-
<button onClick={increaseCounterByOne}>Increase counter by 1</button>
58-
</div>
59-
)
60-
}
61-
```
26+
* import React from 'react'
27+
* import { useActions } from 'react-redux'
28+
*
29+
* const increaseCounter = amount => ({
30+
* type: 'increase-counter',
31+
* amount,
32+
* })
33+
*
34+
* export const CounterComponent = ({ value }) => {
35+
* // supports passing an object of action creators
36+
* const { increaseCounterByOne, increaseCounterByTwo } = useActions({
37+
* increaseCounterByOne: () => increaseCounter(1),
38+
* increaseCounterByTwo: () => increaseCounter(2),
39+
* }, [])
40+
*
41+
* // supports passing an array/tuple of action creators
42+
* const [increaseCounterByThree, increaseCounterByFour] = useActions([
43+
* () => increaseCounter(3),
44+
* () => increaseCounter(4),
45+
* ], [])
46+
*
47+
* // supports passing a single action creator
48+
* const increaseCounterBy5 = useActions(() => increaseCounter(5), [])
49+
*
50+
* // passes through any arguments to the callback
51+
* const increaseCounterByX = useActions(x => increaseCounter(x), [])
52+
*
53+
* return (
54+
* <div>
55+
* <span>{value}</span>
56+
* <button onClick={increaseCounterByOne}>Increase counter by 1</button>
57+
* </div>
58+
* )
59+
* }
6260
*/
6361
export function useActions(actions, deps) {
6462
invariant(actions, `You must pass actions to useActions`)

src/hooks/useDispatch.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ import { useStore } from './useStore'
44
* A hook to access the redux `dispatch` function. Note that in most cases where you
55
* might want to use this hook it is recommended to use `useActions` instead to bind
66
* action creators to the `dispatch` function.
7-
*
7+
*
88
* @returns {any} redux store's `dispatch` function
99
*
10-
* Usage:
10+
* @example
1111
*
12-
```jsx
13-
import React, { useCallback } from 'react'
14-
import { useReduxDispatch } from 'react-redux'
15-
16-
export const CounterComponent = ({ value }) => {
17-
const dispatch = useDispatch()
18-
const increaseCounter = useCallback(() => dispatch({ type: 'increase-counter' }), [])
19-
return (
20-
<div>
21-
<span>{value}</span>
22-
<button onClick={increaseCounter}>Increase counter</button>
23-
</div>
24-
)
25-
}
26-
```
12+
* import React, { useCallback } from 'react'
13+
* import { useReduxDispatch } from 'react-redux'
14+
*
15+
* export const CounterComponent = ({ value }) => {
16+
* const dispatch = useDispatch()
17+
* const increaseCounter = useCallback(() => dispatch({ type: 'increase-counter' }), [])
18+
* return (
19+
* <div>
20+
* <span>{value}</span>
21+
* <button onClick={increaseCounter}>Increase counter</button>
22+
* </div>
23+
* )
24+
* }
2725
*/
2826
export function useDispatch() {
2927
const store = useStore()

src/hooks/useRedux.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@ import { useSelector } from './useSelector'
22
import { useActions } from './useActions'
33

44
/**
5-
* A hook to access the redux store's state and to bind action creators to
5+
* A hook to access the redux store's state and to bind action creators to
66
* the store's dispatch function. In essence, this hook is a combination of
77
* `useSelector` and `useActions`.
8-
*
8+
*
9+
* Note that this hook does currently not allow to pass a dependencies array,
10+
* so the passed selector and any created callbacks are not memoized. If you
11+
* require memoization, please use `useActions` and `useSelector`.
12+
*
913
* @param {Function} selector the selector function
1014
* @param {Function|Function[]|Object.<string, Function>} actions the action creators to bind
11-
*
15+
*
1216
* @returns {[any, any]} a tuple of the selected state and the bound action creators
1317
*
14-
* Usage:
18+
* @example
1519
*
16-
```jsx
17-
import React from 'react'
18-
import { useRedux } from 'react-redux'
19-
20-
export const CounterComponent = () => {
21-
const [counter, { inc1, inc }] = useRedux(state => state.counter, {
22-
inc1: () => ({ type: 'inc1' }),
23-
inc: amount => ({ type: 'inc', amount }),
24-
})
25-
26-
return (
27-
<>
28-
<div>
29-
{counter}
30-
</div>
31-
<button onClick={inc1}>Increment by 1</button>
32-
<button onClick={() => inc(5)}>Increment by 5</button>
33-
</>
34-
)
35-
}
36-
```
20+
* import React from 'react'
21+
* import { useRedux } from 'react-redux'
22+
* import { RootState } from './store'
23+
*
24+
* export const CounterComponent = () => {
25+
* const [counter, { inc1, inc }] = useRedux(state => state.counter, {
26+
* inc1: () => ({ type: 'inc1' }),
27+
* inc: amount => ({ type: 'inc', amount }),
28+
* })
29+
*
30+
* return (
31+
* <>
32+
* <div>
33+
* {counter}
34+
* </div>
35+
* <button onClick={inc1}>Increment by 1</button>
36+
* <button onClick={() => inc(5)}>Increment by 5</button>
37+
* </>
38+
* )
39+
* }
3740
*/
3841
export function useRedux(selector, actions) {
3942
return [useSelector(selector), useActions(actions)]

src/hooks/useReduxContext.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ import { ReactReduxContext } from '../components/Context'
55
/**
66
* A hook to access the value of the `ReactReduxContext`. This is a low-level
77
* hook that you should usually not need to call directly.
8-
*
8+
*
99
* @returns {any} the value of the `ReactReduxContext`
1010
*
11-
* Usage:
11+
* @example
1212
*
13-
```jsx
14-
import React from 'react'
15-
import { useReduxContext } from 'react-redux'
16-
17-
export const CounterComponent = ({ value }) => {
18-
const { store } = useReduxContext()
19-
return <div>{store.getState()}</div>
20-
}
21-
```
13+
* import React from 'react'
14+
* import { useReduxContext } from 'react-redux'
15+
*
16+
* export const CounterComponent = ({ value }) => {
17+
* const { store } = useReduxContext()
18+
* return <div>{store.getState()}</div>
19+
* }
2220
*/
2321
export function useReduxContext() {
2422
const contextValue = useContext(ReactReduxContext)

src/hooks/useSelector.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,23 @@ const useIsomorphicLayoutEffect =
2222
* This hook takes a dependencies array as an optional second argument,
2323
* which when passed ensures referential stability of the selector (this is primarily
2424
* useful if you provide a selector that memoizes values).
25-
*
25+
*
2626
* @param {Function} selector the selector function
2727
* @param {any[]} deps (optional) dependencies array to control referential stability
2828
* of the selector
29-
*
29+
*
3030
* @returns {any} the selected state
3131
*
32-
* Usage:
32+
* @example
3333
*
34-
```jsx
35-
import React from 'react'
36-
import { useSelector } from 'react-redux'
37-
38-
export const CounterComponent = () => {
39-
const counter = useSelector(state => state.counter)
40-
return <div>{counter}</div>
41-
}
42-
```
34+
* import React from 'react'
35+
* import { useSelector } from 'react-redux'
36+
* import { RootState } from './store'
37+
*
38+
* export const CounterComponent = () => {
39+
* const counter = useSelector(state => state.counter, [])
40+
* return <div>{counter}</div>
41+
* }
4342
*/
4443
export function useSelector(selector, deps) {
4544
invariant(selector, `You must pass a selector to useSelectors`)

src/hooks/useStore.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ import { useReduxContext } from './useReduxContext'
22

33
/**
44
* A hook to access the redux store.
5-
*
5+
*
66
* @returns {any} the redux store
77
*
8-
* Usage:
8+
* @example
99
*
10-
```jsx
11-
import React from 'react'
12-
import { useStore } from 'react-redux'
13-
14-
export const CounterComponent = ({ value }) => {
15-
const store = useStore()
16-
return <div>{store.getState()}</div>
17-
}
18-
```
10+
* import React from 'react'
11+
* import { useStore } from 'react-redux'
12+
*
13+
* export const ExampleComponent = () => {
14+
* const store = useStore()
15+
* return <div>{store.getState()}</div>
16+
* }
1917
*/
2018
export function useStore() {
2119
const { store } = useReduxContext()

0 commit comments

Comments
 (0)