4.11.2 • Published 5 years ago
@rooks/use-update-effect v4.11.2
@rooks/use-update-effect
Note: Future updates to this package have moved to the main package rooks. All hooks now reside in a single package which you can install using
npm install rooksor
yarn add rooksRooks is completely treeshakeable and if you use only 1 of the 50+ hooks in the package, only that hook will be bundled with your code. Your bundle will only contain the hooks that you need. Cheers!
About
An useEffect that does not run on first render
Installation
npm install --save @rooks/use-update-effectImporting the hook
import useUpdateEffect from "@rooks/use-update-effect";Usage
function Demo() {
const [userID, setUserID] = useState();
const [hasUpdated, setHasUpdated] = useState({ userID, updated: false });
useUpdateEffect(() => {
API.subscribe(userID);
setHasUpdated({ userID, updated: true });
() => {
API.unsubscribe(userID);
setHasUpdated({ userID, updated: false });
};
}, [value]);
return (
<>
<button onClick={() => setUserID(Math.random())}>user ID is {userID}</button>
<p>Has updated for userID - {hasUpdated.toString()}</p>
</>
);
}
render(<Demo />);