useUpdateEffect
React effect hook that ignores the first invocation (e.g. on mount). The signature is exactly the same as the useEffect
hook.
Usage
const Demo = () => {
const [count, setCount] = useState(0);
const [effectCount, setEffectCount] = useState(0);
const [updateEffectCount, setUpdateEffectCount] = useState(0);
useEffect(() => {
setEffectCount(c => c + 1);
}, [count]);
useUpdateEffect(() => {
setUpdateEffectCount(c => c + 1);
return () => {
// do something
};
}, [count]); // you can include deps array if necessary
return (
<div>
<p>effectCount: {effectCount}</p>
<p>updateEffectCount: {updateEffectCount}</p>
<p>
<button type="button" onClick={() => setCount(c => c + 1)}>
reRender
</button>
</p>
</div>
);
};
Example
effectCount: 0
updateEffectCount: 0
Type Declarations
declare const _default: typeof useEffect | typeof useLayoutEffect
export default _default