Git Product home page Git Product logo

Comments (9)

pkellner avatar pkellner commented on May 18, 2024

I think I figure it out. You call the cleanup when the task manager is replaced and that aborts.

from react-hooks-async.

dai-shi avatar dai-shi commented on May 18, 2024

Yeah, we abort a previous task here (regardless whether it's running or not).

if (previous.current) {
previous.current.abort();
}

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.

pkellner avatar pkellner commented on May 18, 2024

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.

dai-shi avatar dai-shi commented on May 18, 2024

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.

pkellner avatar pkellner commented on May 18, 2024

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.

dai-shi avatar dai-shi commented on May 18, 2024

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.

pkellner avatar pkellner commented on May 18, 2024

Good catch. Otherwise, you believe I'm handling the cancelation token correctly? That's the part that always confused me.

from react-hooks-async.

dai-shi avatar dai-shi commented on May 18, 2024

Guess so.

Some other notes:

  • calling source() out of useEffect 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.

dai-shi avatar dai-shi commented on May 18, 2024

Closing this issue. Feel free to open a new one.

from react-hooks-async.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.