All Projects → PedroBern → React Hook Use Cancelable Thunk Reducer

PedroBern / React Hook Use Cancelable Thunk Reducer

Licence: mit
React Hook useReducer with cancelable dispatch of thunks

Programming Languages

javascript
184084 projects - #8 most used programming language

use-cancelable-thunk-reducer

Codecov Coverage Build Status dependencies

Custom implementation of react hook useReducer that will cancel all dispatched actions if the component is unmounted and allows to dispatch thunk actions (that will be canceled either).

demo-gif

Open on codesanbox.

Installation

yarn add use-cancelable-thunk-reducer

npm i use-cancelable-thunk-reducer

useCancelableThunkReducer

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.

const thunkAction = args => async (dispatch, getState) => {
  dispatch({type: ACTION_SENT});
  const state = getState();
  ...
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].