Registers a side effect with the same signature as React's useEffect
, but the effect will only execute when the manual trigger function returned by this hook is invoked.
A few use-cases for this hook include:
- Deferring an effect to the next render tick.
- Sequencing asynchronous effects.
- Executing an effect after marshalling arbitrary state/data required for the effect to proceed.
This hook is a bit esoteric, so it helps to see how it can be used in context. For reference, usable-react
makes use of this hook internally within useDebouncedEffect
and useInterval
.
const triggerEffect = useEffectTrigger(() => {
// ...
}, [/* effect dependencies */]);
// Somewhere else in the component...
triggerEffect();