forked from reduxjs/react-redux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useDispatch.js
29 lines (28 loc) · 852 Bytes
/
useDispatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
*
* @example
*
* 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()
return store.dispatch
}