-
Notifications
You must be signed in to change notification settings - Fork 0
What is the best practice to cancel an already invoked asynchronous callback when component is already unmounted? #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As far as I know the boolean flag to signal cancelation is the only way to "cancel" (native) promises right now. There's also https://github.com/petkaantonov/bluebird#note which is an alternative implementation to promises and supports cancelation. However, all functions which return native promises need to return the alternative implementation, meaning we can't use After diving into SWR/react-query's [1] approach to canceling requests it looks like cancelling promises is just one side of the issue. Cancelling promises only avoids calling the It looks like we're currently not using If request cancellation isn't an issue (only promise cancellation), SWR/react-query could indeed be used to avoid the unmounting issue by hiding manual promise creations into a function useDownloadStatus(track: Track) {
return useQuery<DownloadStatus>(["downloadStatus", trackId], function() {
const { exists } = await FileSystem.getInfoAsync(getTrackFileUri(track));
// some mapping
return { state: ..., progress: ... };
}, {staleTime: 0, refetchEvery: 10});
})
// React component
function DownloadStatus(props: { track: Track} {
const { data, loading } = useDownloadStatus(props.track);
<>{data && data.state === IS_DOWNLOADING && <DownloadProgress/>}</> This would get rid of the syncing between the If we need to fetch the array of tracks with their corresponding statuses, we can also use [1] react-query fills the same niche as SWR and I've used it in the past. Worked great, devtools not yet available on react-native, though. They've been awesome to figure out why stuff was cached/refetched/etc. |
Thanks for the thorough suggestions. Request cancellation is actually not an issue because I like the idea of |
Uh oh!
There was an error while loading. Please reload this page.
I did it with a
isMounted
flag inyoga-insights/app/components/download-switch/download-switch.tsx
Lines 282 to 318 in 566b4b0
Related to #35
The text was updated successfully, but these errors were encountered: