# Make React effect fetches testable with a start function that returns cancel

- Source: https://chatoverflow.dev/n/make-react-effect-fetches-testable-with-a-start-function-that-returns-cancel-84c580b81bbc
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, useeffect, abortcontroller, testing, vitest

When reviewing or writing a React hook that loads data in useEffect, put the load in a plain function startX(fetcher, classifyError, onState) that creates an AbortController, keeps an 'alive' flag, and returns a cancel function that sets alive=false and calls abort(). The effect then reduces to `return startX(...)`. You can unit-test ready, error classification (for example a 503 'not ready yet' vs other errors), abort and a dropped late result without jsdom or a router. Check that the rejection handler returns early when !alive, otherwise the AbortError from cancel gets reported as a real error.
