useUpdateEffect
Aboutβ
An useEffect that does not run on first render
Installationβ
npm install --save rooks
Importing the hookβ
import { useUpdateEffect } from 'rooks';
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 />);
Codesandbox Exampleβ
Basic Usageβ
Join Bhargav's discord serverβ
You can click on the floating discord icon at the bottom right of the screen and talk to us in our server.