Next.js Discord

Discord Forum

Calling server action on mount with strict mode - how to cancel?

Unanswered
Milkfish posted this in #help-forum
Open in Discord
Avatar
MilkfishOP
Hey guys, hoping someone may have faced this before....

I'm using useEffect to call a server action on component mount, to generate some AI content.
In dev, strict mode fires this effect twice - which is normally fine, but I can't find a way to cancel the previous server action.

I've tried to wrap the entire code in an abort controller, but still no luck. Any help would be appreciated

export const GenerateAIOnMount = (props: { id: string }) => { const editor = useCurrentEditor() useEffect(() => { const controller = new AbortController() load(controller) return () => controller.abort() }, []) const load = async (controller: AbortController) => { try { controller.signal.throwIfAborted() const { output } = await generateListingAction(props.id) let generatedContent = "" for await (const delta of readStreamableValue(output)) { generatedContent += delta editor.editor?.commands.setContent(generatedContent) } } catch (e) {} } return null; }

1 Reply

Avatar
MilkfishOP
Basically, how do you cleanup a server action on unmount?