use-effect-skip-first
v0.1.3
Published
Skip the first run of a React `useEffect`—handy for ignoring the initial mount and only reacting to subsequent dependency changes.
Downloads
29
Maintainers
Readme
use-effect-skip-first
Skip the first run of a React useEffect—handy for ignoring the initial mount and only reacting to subsequent dependency changes.
Install
pnpm add use-effect-skip-first
# or
npm i use-effect-skip-firstUsage
import * as React from "react";
import { useState } from "react";
import { useEffectSkipFirst } from "use-effect-skip-first";
export default function App() {
const [counter, setCounter] = useState(0);
useEffectSkipFirst(() => {
console.log("effect called", counter);
}, [counter]);
return (
<div>
<h1>use-effect-skip-first Demo</h1>
<button onClick={() => setCounter(counter + 1)}>Click me</button>
<p>Counter: {counter}</p>
</div>
);
}API
useEffectSkipFirst(effect: EffectCallback, deps?: DependencyList): void;- Skips the effect on the initial render when you provide a non-empty
depsarray. - On later updates (when any dep changes), your
effectruns as usual.
Notes
- If
depsis empty or omitted, the effect behaves like a normaluseEffect(it will run on mount). - Return a cleanup function from
effectif needed—identical to the standarduseEffectAPI.
