
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
use-current-effect
Advanced tools
Sometimes we need to track if an effect has been cleaned up, because one of it's dependencies has changed, or the component was unmounted. The useCurrentEffect
hook gives us a helper parameter to track this state without the usual boilerplate.
npm i use-current-effect
or just copy the hook from this repo
Dan Abramov shows us how we can track if the effect has been "cancelled" here https://overreacted.io/a-complete-guide-to-useeffect/#speaking-of-race-conditions
useEffect(() => {
let didCancel = false;
async function fetchData() {
const article = await API.fetchArticle(id);
if (!didCancel) {
setArticle(article);
}
}
fetchData();
return () => {
didCancel = true;
};
}, [id]);
With useCurrentEffect
you can do away with this boilerplate and clean up your effects...
useCurrentEffect((effectState) => {
async function fetchData() {
const article = await API.fetchArticle(id);
if (effectState.isCurrent) {
setArticle(article);
}
}
fetchData();
}, [id]);
Any regular clean up function returned by the first effect parameter function will still be called after the ìsCurrent
flag has been set, so you may still unsubscribe from subscriptions, or do other cleanup as usual.
Note that deconstructing the parameter as ({ isCurrent })
will cause issues, because doing so effectively creates a copy of boolean field, scoped to the effect function only, which will not be mutated by the cleanup.
If you use the ESLint rule react-hooks/exhaustive-deps
then you can add the useCurrentEffect
to your additionalHooks
Regex in your .eslint
to ensure that you don't miss any dependencies.
"react-hooks/exhaustive-deps": ["warn", { "additionalHooks": "useCurrentEffect" }],
FAQs
useEffect hook with injected lifecycle checking method
The npm package use-current-effect receives a total of 31,623 weekly downloads. As such, use-current-effect popularity was classified as popular.
We found that use-current-effect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.