-
Notifications
You must be signed in to change notification settings - Fork 27
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
In TypeAhead Demo, I don't see how abort is being called when query changes #3
Comments
I think I figure it out. You call the cleanup when the task manager is replaced and that aborts. |
Yeah, we abort a previous task here (regardless whether it's running or not). react-hooks-async/src/use-async-run.js Lines 7 to 9 in 6f0e2b3
Hm, I don't remember why I didn't use cleanup in useEffect... |
double hm. I'm thinking that in https://github.com/dai-shi/react-hooks-async/blob/master/src/use-async-task.js, you have a cleanup and that gets called automatically when you create an new task on top of an old task. That is, if the create task call is in the render method, and the render method gets called again because of a query change, the new task is created, the old task is destroyed and clean up automatically runs causing the signal to be fired, and the running task to be aborted. Maybe I just made all that up, but I put some logging in and that is what I think is happening. |
The reason why they are separated is to allow combining tasks by I guess you saw the behavior that |
Would you possibly have a moment to look and see if I handled axios request cancel correctly here. I did it in cleanup but it seems not to be working correctly. |
You should not update state after unmounted. Just coding in my head: const useAxiosGet = url => {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
let isMounted = true;
const source = axios.CancelToken.source();
axios.get(url, { cancelToken: source.token })
.then(data => {
if (isMounted) {
setData(data);
setLoading(false);
}
})
.catch(e => {
if (isMounted) {
setError(e);
setLoading(false);
}
});
return () => {
isMounted = false;
source.cancel('cancel');
};
}, []);
return { data, error, loading };
}; |
Good catch. Otherwise, you believe I'm handling the cancelation token correctly? That's the part that always confused me. |
Guess so. Some other notes:
|
Closing this issue. Feel free to open a new one. |
I'm worried that you are not aborting you previous request when the query string is changed. It seems that you are just creating another task. I only see the .abort() called when you explicitly cancel by pressing the button.
Am I mis-understanding? seems like you'd need some kind of timer running and then when the query string changes, you'd need to abort the request and create another one (rather than just creating another one).
The text was updated successfully, but these errors were encountered: