diff --git a/hooks/use-batch-create.ts b/hooks/use-batch-create.ts index 72f1fa7..67f793c 100644 --- a/hooks/use-batch-create.ts +++ b/hooks/use-batch-create.ts @@ -1,6 +1,6 @@ 'use client' -import { useState, useCallback, useRef } from 'react' +import { useState, useCallback, useRef, useEffect } from 'react' import { createStream as createStreamCall } from '@/lib/contract' import { invalidateStreams } from '@/hooks/use-streams' import { useWallet } from '@/hooks/use-wallet' @@ -204,5 +204,14 @@ export function useBatchCreate() { abortRef.current = true }, []) + // Automatically abort the running batch when the component unmounts so that + // in-progress state updates are not posted to an unmounted component. + // The cancel callback is stable (empty deps) so this effect runs only once. + useEffect(() => { + return () => { + abortRef.current = true + } + }, []) + return { progress, createBatch, retryFailed, cancel } }