How can resources be cleaned-up when using Suspense?

51 views Asked by At

React async code is usually written along these lines (when clean-up is desired):

useEffect(() => {
  const controller = new AbortController();
  const signal = controller.signal;
  const promise = getResource(id, { signal })

  // set data when complete
  promise.then(data => setData(data));

  // Cancel operation if need to clean-up
  return () => controller.abort();
}, [getResource, id])

How can something similar be achieved with Suspense?

Do I need to write a pure clean-up effect for my clean-up needs, separate from suspense?

This applies not just to aborted requests, but also disconnecting from socket connections, leaving channels, etc.

0

There are 0 answers