Or we can use a bit of magic :) (Live Demo)
import { useAsyncEffect } from "use-async-effect2"; import cpAxios from "cp-axios"; function TestComponent({url}) { const [cancel, done, result, err] = useAsyncEffect( function* () { return (yield cpAxios(url)).data; }, { states: true } ); return ( <div> {done ? (err ? err.toString() : JSON.stringify(result)) : "loading..."} <button className="btn btn-warning" onClick={cancel} disabled={done}> Cancel async effect (abort request) </button> </div> ); }
I definitely have to check Axios out!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Or we can use a bit of magic :) (Live Demo)
I definitely have to check Axios out!