Comments (9)
I think I figure it out. You call the cleanup when the task manager is replaced and that aborts.
from react-hooks-async.
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...
It should be better because it aborts task on unmount.
from react-hooks-async.
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.
from react-hooks-async.
useAsyncTask
creates an async task (object) but it doesn't run nor abort by itself.
useAsyncRun
is the actual one that has a control of running and aborting.
The reason why they are separated is to allow combining tasks by useAsyncCombine*
.
I guess you saw the behavior that useAsyncRun
triggers the aborting and then it actually aborts in useAsyncTask
in the logging.
from react-hooks-async.
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.
https://stackoverflow.com/q/53861916/46942
from react-hooks-async.
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 };
};
from react-hooks-async.
Good catch. Otherwise, you believe I'm handling the cancelation token correctly? That's the part that always confused me.
from react-hooks-async.
Guess so.
Some other notes:
- calling
source()
out ofuseEffect
doesn't make sense. it won't be used even after newly created. - try-catch is probablly unnecessary if you use promise.
- setLoading in useEffect is redundant.
- this hook is one-time only, meaning it doesn't follow the change of
url
. window.location=
may not unmount the component but just leave the page entirely.
from react-hooks-async.
Closing this issue. Feel free to open a new one.
from react-hooks-async.
Related Issues (20)
- How to prevent an initial run HOT 11
- importing from src HOT 10
- infinite re-rendering when calling useAsyncTaskFetch hook HOT 2
- using asyncTask without useEffect HOT 9
- useFetch: Infinite re-rendering only when NODE_ENV=production HOT 12
- 3.1.0 breaks conditional running with useAsyncRun HOT 3
- useAsyncTaskFetch: pending is "true" from the start HOT 15
- Issue with tests and .start() since 3.2.0 HOT 2
- useAsyncTaskFetch doesn't provide access the response body if server returns error HOT 6
- what is the purpose of the started flag HOT 3
- Return a promise from task.start() HOT 5
- Support for react-native HOT 3
- Should we throw an error when fetch is aborted? HOT 4
- Typescript errors HOT 3
- How to handle FetchError? HOT 9
- Chaining async fetches HOT 2
- Error without statusText via HTTP/2 protocol HOT 7
- handling abort in useAsyncTask start().then HOT 2
- Unhandled promise rejection error HOT 6
- Project maintenance and future of data fetching HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-hooks-async.