https://github.com/pedrobern/react-hook-use-cancelable-thunk-reducer
React Hook useReducer with cancelable dispatch of thunks
https://github.com/pedrobern/react-hook-use-cancelable-thunk-reducer
react react-hooks redux thunk usereducer
Last synced: 3 months ago
JSON representation
React Hook useReducer with cancelable dispatch of thunks
- Host: GitHub
- URL: https://github.com/pedrobern/react-hook-use-cancelable-thunk-reducer
- Owner: PedroBern
- License: mit
- Created: 2019-12-15T18:03:27.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T03:03:47.000Z (almost 3 years ago)
- Last Synced: 2025-08-06T05:49:33.885Z (4 months ago)
- Topics: react, react-hooks, redux, thunk, usereducer
- Language: JavaScript
- Size: 831 KB
- Stars: 26
- Watchers: 1
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# use-cancelable-thunk-reducer
[](https://codecov.io/gh/pedrobern/react-hook-use-cancelable-thunk-reducer/)
[](https://travis-ci.com/pedrobern/react-hook-use-cancelable-thunk-reducer)
[](https://github.com/pedrobern/react-hook-use-cancelable-thunk-reducer)
Custom implementation of react hook `useReducer` that will cancel all dispatched actions if the component is unmounted and allows to dispatch [thunk actions](https://github.com/reduxjs/redux-thunk) (that will be canceled either).

Open on [codesanbox](https://codesandbox.io/s/use-cancelable-thunk-reducer-lirs9).
## Installation
```
yarn add use-cancelable-thunk-reducer
npm i use-cancelable-thunk-reducer
```
## useCancelableThunkReducer
```javascript
import useCancelableThunkReducer from 'use-cancelable-thunk-reducer';
const [state, dispatch] = useCancelableThunkReducer(
reducer,
initialState,
callback,
init
);
```
### `reducer`
useReducer first argument.
### `initialState`
useReducer second argument.
### `callback`
default is `undefined`, if is a `function`, when some action is canceled it is called with the action argument: `callback(action)`.
### `init`
useReducer last argument.
## Thunk action
The thunk actions receive `(dispatch, getState)` args.
```javascript
const thunkAction = args => async (dispatch, getState) => {
dispatch({type: ACTION_SENT});
const state = getState();
...
}
```