useDidUpdateEffect
A custom hook that skips execution on the initial mount and only runs the effect when the dependency array (deps) values are updated.
Code
Interface
typescript
function useDidUpdateEffect(effect: EffectCallback, deps: DependencyList): void
Parameters
| Name | Type | Description |
|---|---|---|
effectCallback | EffectCallback | The effect function to execute |
deps | DependencyList | Dependency array that re-runs the effect when changed |
Usage
typescript
import { useDidUpdateEffect } from '@modern-kit/react'
const Example = () => {
const [count, setCount] = useState(0);
const [isUpdated, setIsUpdated] = useState(false);
useDidUpdateEffect(() => {
setIsUpdated(true);
}, [count]);
return (
<div>
<button onClick={() => setCount(count + 1)}>Increment: {count}</button>
{isUpdated && <p>count has been updated. {count}</p>}
</div>
);
}