question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add usage example with Axios

See original GitHub issue

Axios is a popular alternative to fetch. We should provide document how to use React Async with Axios, including Axios’ cancellation mechanism.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
ghengeveldcommented, Jun 20, 2019

That’s certainly possible. It would be very similar to how useFetch is implemented, but with Axios instead of fetch. It won’t be part of React Async though, because I don’t want to add a dependency. Adding it as an example is fine though 👍

Maybe you can pick that up? Here’s the outline:

const useAxios = (arg1, arg2, { defer, ...options }) => {
  const config = typeof arg1 === "string" ? { url: arg1, ...arg2 } : arg1
  const method = config.method && config.method.toUpperCase()
  const isDefer = defer === true || ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
  const fn = defer === false || !isDefer ? "promiseFn" : "deferFn"
  const source = axios.CancelToken.source()
  const state = useAsync({
    ...options,
    [fn]: useCallback(() => axios({ cancelToken: source.token, ...config }), [
      JSON.stringify(config),
    ]),
    onReject: e => {
      e.name === "AbortError" && source.cancel(e.message)
      return options.onReject && options.onReject(e)
    },
  })
  useDebugValue(state, ({ counter, status }) => `[${counter}] ${status}`)
  return state
}

This is untested, but it should be close enough.

3reactions
ghengeveldcommented, Jul 20, 2019

The onCancel callback was recently added in v7.0.2.

An extension like react-async-axios is certainly possible, but not really necessary I think. With onCancel, it would be something like this:

const useAxios = (arg1, arg2, { defer, ...options }) => {
  const config = typeof arg1 === "string" ? { url: arg1, ...arg2 } : arg1
  const method = config.method && config.method.toUpperCase()
  const isDefer = defer === true || ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
  const fn = defer === false || !isDefer ? "promiseFn" : "deferFn"
  const source = axios.CancelToken.source()
  const state = useAsync({
    ...options,
    [fn]: useCallback(() => axios({ cancelToken: source.token, ...config }), [
      JSON.stringify(config),
    ]),
    onCancel: () => {
      source.cancel()
	  options.onCancel && options.onCancel()
    },
  })
  useDebugValue(state, ({ counter, status }) => `[${counter}] ${status}`)
  return state
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios tutorial - GET/POST requests in JavaScript with Axios
In the first example, we create a simple GET request. We use callbacks. ... const axios = require('axios'); axios.get('http://webcode.me').then( ...
Read more >
Making HTTP requests with Axios - CircleCI
Axios is a promise-based HTTP library that lets developers make requests to either their own or a third-party server to fetch data.
Read more >
How to make HTTP requests with Axios - LogRocket Blog
In this tutorial, we'll demonstrate how to make HTTP requests using Axios with clear examples, including how to make an Axios POST request...
Read more >
How To Use Axios with React - DigitalOcean
In this article, you will see examples of how to use Axios to access the popular JSON Placeholder API within a React application....
Read more >
Complete Guide to Axios HTTP Client - Reflectoring
In this article, we will understand Axios and use its capabilities to make different types of REST API calls from JavaScript applications.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found