1.0.0 • Published 5 years ago

use-safe-effect-hook v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

useSafeEffect

A hook designed to perform an async effect in a safe way, thus prevent possible and undesired race condition.

Installation

yarn add use-safe-effect-hook

Usage

useSafeEffect(
  ({ checkEffectValidity, handleEffectError }) => {
    anAsyncAction(id)
      .then(checkEffectValidity)
      // state is updated only if effect is still valid
      .then(updateState)
      // error is shown only if effect is still valid
      .catch(handleEffectError(showError))
  },
  [id]
)