dispatch-resolve
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Dispatch Resolve

useDispatchResolve

useDispatchResolve is a custom hook helping to synchronously execute the statement after dispatching a redux side effect action when using saga or thunk middleware.

Usage

useDispatchResolve() return a dispatch function like useDispatch():

const dispatchResolve = useDispatchResolve();

dispatchResolve(action) will return a promise:

await dispatchResolve(action); // OR dispatchResolve(action).then(...) as well
// others statements
Importance Note

When using dispatchResolve, your action will be add a new property "resolver" that contains resolve and reject function of a new Promise (check source code to get to know more). Use that "resolver" to decide that whenever resolve the promise then execute next statement OR reject the promise.

Example:

function* abcSaga(action) {
  const { resolver = {} } = action; // Be careful, resolver will be undefined if using useDispatch of react-redux
  try {
    const result = yield axios.get(...);
    typeof resolver.resolve === 'function' && resolver.resolve();
  } catch () {
    typeof resolver.reject === 'function' && resolver.reject();
  }
}

Dependencies (0)

    Dev Dependencies (10)

    Package Sidebar

    Install

    npm i dispatch-resolve

    Weekly Downloads

    1

    Version

    1.0.1

    License

    ISC

    Unpacked Size

    3.99 kB

    Total Files

    4

    Last publish

    Collaborators

    • pynhpo