Skip to content

jungai/cancel-request-in-react

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Cancel Request In React

post here

in main page (app.tsx) try to switch component WithAbort and WithOutAbort

  • Test component is not handle canceling request might be a error when component unmounted
  • Fix component is handle canceling request by using AbortController see below

use AbortController(browser support) for cannel request

Testing on http client lib and normal fetch(browser)

fetch

adding signal opts in fetch

const rawData = await fetch("https://jsonplaceholder.typicode.com/todos/", {
  signal: controller.signal,
});
const data = await rawData.json();

axios

adding signal opts in axios

const data = await axios.get("https://jsonplaceholder.typicode.com/todos", {
  signal: controller.signal,
});

ky might be a bug ??

refs

const data = await ky
  .get("https://jsonplaceholder.typicode.com/todos", {
    signal: controller.signal,
  })
  .json(); // not working as expected

see example in react useEffect here

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published