From d535901405f7125537164c99876daf4c9554b8a4 Mon Sep 17 00:00:00 2001 From: Chibey-max Date: Sat, 25 Jul 2026 08:44:57 +0100 Subject: [PATCH] fix(hooks): add unmount cleanup to useBatchCreate useBatchCreate started a batch operation but had no cleanup on unmount, so the running batch would continue posting state updates to an already- unmounted component. Add a useEffect that sets abortRef.current = true on cleanup, mirroring what the existing cancel() callback does. The abortRef flag is already checked at the top of every loop iteration in both createBatch and retryFailed, so setting it on unmount is sufficient to stop any in-progress batch without further changes. Closes #274 --- hooks/use-batch-create.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 } }