Skip to content

honzabrecka/useSafeEffect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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]
)

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published