Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
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 function as a parameter to track this state without the usual boilerplate.
npm i use-current-effect
import { useCurrentEffect } from "use-current-effect";
// ...
useCurrentEffect((isCurrent) => {
async function fetchData() {
const article = await API.fetchArticle(id);
if (isCurrent()) {
setArticle(article);
}
}
fetchData();
}, [id]);
You could do this manually like this each time you want to make this check:
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 make your effects more consise.
There is also useCurrentCallback
which works in a similar way, however as the consumer of the hook may want to pass parameters to the callback function, we must use a slightly different pattern. useCurrentCallback
takes a generator function so you may inject the checker function.
const onSearchOrders = useCurrentCallback(
isCurrent => searchParams => {
api.searchOrders(customerId, searchParams).then(results => {
if (isCurrent()) {
setSearchResults(results);
}
});
},
[customerId]
);
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 22,929 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.