Skip to content
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

useDataApi: Is it worth to add axios.CancelToken ? #16

Open
albertly opened this issue Jan 26, 2021 · 0 comments
Open

useDataApi: Is it worth to add axios.CancelToken ? #16

albertly opened this issue Jan 26, 2021 · 0 comments

Comments

@albertly
Copy link

Something like this?

 useEffect(() => {
    const cancelTokenSource = axios.CancelToken.source();

    const fetchData = async () => {
      dispatch({ type: 'FETCH_INIT' });

      try {
        const result = await await axios(url, {
            cancelToken: cancelTokenSource.token,
          });

        if (!axios.isCancel()) {
          dispatch({ type: 'FETCH_SUCCESS', payload: result.data });
        }
      } catch (error) {
        if (!axios.isCancel(error)) {
          dispatch({ type: 'FETCH_FAILURE' });
        }
      }
    };

    fetchData();

    return () => {
      cancelTokenSource.cancel();
    };
  }, [url]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant