Dùng AbortController để cancel fetch request cũ khi effect re-run: const controller = new AbortController(); fetch(url, { signal: controller.signal }); return () => controller.abort().
- Hoặc dùng boolean flag
let cancelled = falsetrong cleanup. - React Query và SWR tự động handle race conditions.
Use AbortController to cancel the previous request when the effect re-runs: const controller = new AbortController(); fetch(url, { signal: controller.signal }); return () => controller.abort().
- Alternatively, use a boolean cancelled flag and check it in the cleanup.
- React Query and SWR handle race conditions automatically and are the recommended choice for production apps.