Skip to content

use proper example code tag in hooks JSDoc comments #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 41 additions & 43 deletions src/hooks/useActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,57 @@ import { useMemo } from 'react'
/**
* A hook to bind action creators to the redux store's `dispatch` function
* similar to how redux's `bindActionCreators` works.
*
*
* Supports passing a single action creator, an array/tuple of action
* creators, or an object of action creators.
*
*
* Any arguments passed to the created callbacks are passed through to
* the your functions.
*
* your functions.
*
* This hook takes a dependencies array as an optional second argument,
* which when passed ensures referential stability of the created callbacks.
*
*
* @param {Function|Function[]|Object.<string, Function>} actions the action creators to bind
* @param {any[]} deps (optional) dependencies array to control referential stability
*
*
* @returns {Function|Function[]|Object.<string, Function>} callback(s) bound to store's `dispatch` function
*
* Usage:
* @example
*
```jsx
import React from 'react'
import { useActions } from 'react-redux'

const increaseCounter = ({ amount }) => ({
type: 'increase-counter',
amount,
})

export const CounterComponent = ({ value }) => {
// supports passing an object of action creators
const { increaseCounterByOne, increaseCounterByTwo } = useActions({
increaseCounterByOne: () => increaseCounter(1),
increaseCounterByTwo: () => increaseCounter(2),
}, [])

// supports passing an array/tuple of action creators
const [increaseCounterByThree, increaseCounterByFour] = useActions([
() => increaseCounter(3),
() => increaseCounter(4),
], [])

// supports passing a single action creator
const increaseCounterBy5 = useActions(() => increaseCounter(5), [])

// passes through any arguments to the callback
const increaseCounterByX = useActions(x => increaseCounter(x), [])

return (
<div>
<span>{value}</span>
<button onClick={increaseCounterByOne}>Increase counter by 1</button>
</div>
)
}
```
* import React from 'react'
* import { useActions } from 'react-redux'
*
* const increaseCounter = amount => ({
* type: 'increase-counter',
* amount,
* })
*
* export const CounterComponent = ({ value }) => {
* // supports passing an object of action creators
* const { increaseCounterByOne, increaseCounterByTwo } = useActions({
* increaseCounterByOne: () => increaseCounter(1),
* increaseCounterByTwo: () => increaseCounter(2),
* }, [])
*
* // supports passing an array/tuple of action creators
* const [increaseCounterByThree, increaseCounterByFour] = useActions([
* () => increaseCounter(3),
* () => increaseCounter(4),
* ], [])
*
* // supports passing a single action creator
* const increaseCounterBy5 = useActions(() => increaseCounter(5), [])
*
* // passes through any arguments to the callback
* const increaseCounterByX = useActions(x => increaseCounter(x), [])
*
* return (
* <div>
* <span>{value}</span>
* <button onClick={increaseCounterByOne}>Increase counter by 1</button>
* </div>
* )
* }
*/
export function useActions(actions, deps) {
invariant(actions, `You must pass actions to useActions`)
Expand Down
32 changes: 15 additions & 17 deletions src/hooks/useDispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,24 @@ import { useStore } from './useStore'
* A hook to access the redux `dispatch` function. Note that in most cases where you
* might want to use this hook it is recommended to use `useActions` instead to bind
* action creators to the `dispatch` function.
*
*
* @returns {any} redux store's `dispatch` function
*
* Usage:
* @example
*
```jsx
import React, { useCallback } from 'react'
import { useReduxDispatch } from 'react-redux'

export const CounterComponent = ({ value }) => {
const dispatch = useDispatch()
const increaseCounter = useCallback(() => dispatch({ type: 'increase-counter' }), [])
return (
<div>
<span>{value}</span>
<button onClick={increaseCounter}>Increase counter</button>
</div>
)
}
```
* import React, { useCallback } from 'react'
* import { useReduxDispatch } from 'react-redux'
*
* export const CounterComponent = ({ value }) => {
* const dispatch = useDispatch()
* const increaseCounter = useCallback(() => dispatch({ type: 'increase-counter' }), [])
* return (
* <div>
* <span>{value}</span>
* <button onClick={increaseCounter}>Increase counter</button>
* </div>
* )
* }
*/
export function useDispatch() {
const store = useStore()
Expand Down
53 changes: 28 additions & 25 deletions src/hooks/useRedux.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@ import { useSelector } from './useSelector'
import { useActions } from './useActions'

/**
* A hook to access the redux store's state and to bind action creators to
* A hook to access the redux store's state and to bind action creators to
* the store's dispatch function. In essence, this hook is a combination of
* `useSelector` and `useActions`.
*
*
* Note that this hook does currently not allow to pass a dependencies array,
* so the passed selector and any created callbacks are not memoized. If you
* require memoization, please use `useActions` and `useSelector`.
*
* @param {Function} selector the selector function
* @param {Function|Function[]|Object.<string, Function>} actions the action creators to bind
*
*
* @returns {[any, any]} a tuple of the selected state and the bound action creators
*
* Usage:
* @example
*
```jsx
import React from 'react'
import { useRedux } from 'react-redux'

export const CounterComponent = () => {
const [counter, { inc1, inc }] = useRedux(state => state.counter, {
inc1: () => ({ type: 'inc1' }),
inc: amount => ({ type: 'inc', amount }),
})

return (
<>
<div>
{counter}
</div>
<button onClick={inc1}>Increment by 1</button>
<button onClick={() => inc(5)}>Increment by 5</button>
</>
)
}
```
* import React from 'react'
* import { useRedux } from 'react-redux'
* import { RootState } from './store'
*
* export const CounterComponent = () => {
* const [counter, { inc1, inc }] = useRedux(state => state.counter, {
* inc1: () => ({ type: 'inc1' }),
* inc: amount => ({ type: 'inc', amount }),
* })
*
* return (
* <>
* <div>
* {counter}
* </div>
* <button onClick={inc1}>Increment by 1</button>
* <button onClick={() => inc(5)}>Increment by 5</button>
* </>
* )
* }
*/
export function useRedux(selector, actions) {
return [useSelector(selector), useActions(actions)]
Expand Down
20 changes: 9 additions & 11 deletions src/hooks/useReduxContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import { ReactReduxContext } from '../components/Context'
/**
* A hook to access the value of the `ReactReduxContext`. This is a low-level
* hook that you should usually not need to call directly.
*
*
* @returns {any} the value of the `ReactReduxContext`
*
* Usage:
* @example
*
```jsx
import React from 'react'
import { useReduxContext } from 'react-redux'

export const CounterComponent = ({ value }) => {
const { store } = useReduxContext()
return <div>{store.getState()}</div>
}
```
* import React from 'react'
* import { useReduxContext } from 'react-redux'
*
* export const CounterComponent = ({ value }) => {
* const { store } = useReduxContext()
* return <div>{store.getState()}</div>
* }
*/
export function useReduxContext() {
const contextValue = useContext(ReactReduxContext)
Expand Down
23 changes: 11 additions & 12 deletions src/hooks/useSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ const useIsomorphicLayoutEffect =
* This hook takes a dependencies array as an optional second argument,
* which when passed ensures referential stability of the selector (this is primarily
* useful if you provide a selector that memoizes values).
*
*
* @param {Function} selector the selector function
* @param {any[]} deps (optional) dependencies array to control referential stability
* of the selector
*
*
* @returns {any} the selected state
*
* Usage:
* @example
*
```jsx
import React from 'react'
import { useSelector } from 'react-redux'

export const CounterComponent = () => {
const counter = useSelector(state => state.counter)
return <div>{counter}</div>
}
```
* import React from 'react'
* import { useSelector } from 'react-redux'
* import { RootState } from './store'
*
* export const CounterComponent = () => {
* const counter = useSelector(state => state.counter, [])
* return <div>{counter}</div>
* }
*/
export function useSelector(selector, deps) {
invariant(selector, `You must pass a selector to useSelectors`)
Expand Down
20 changes: 9 additions & 11 deletions src/hooks/useStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ import { useReduxContext } from './useReduxContext'

/**
* A hook to access the redux store.
*
*
* @returns {any} the redux store
*
* Usage:
* @example
*
```jsx
import React from 'react'
import { useStore } from 'react-redux'

export const CounterComponent = ({ value }) => {
const store = useStore()
return <div>{store.getState()}</div>
}
```
* import React from 'react'
* import { useStore } from 'react-redux'
*
* export const ExampleComponent = () => {
* const store = useStore()
* return <div>{store.getState()}</div>
* }
*/
export function useStore() {
const { store } = useReduxContext()
Expand Down