Skip to content

Commit d3e8909

Browse files
committed
Form dialog: don't update state after unmount
1 parent a853e27 commit d3e8909

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎console/components/console/form-dialog.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client"
22

3-
import { useRef, useState, type ReactNode } from "react"
3+
import { useEffect, useRef, useState, type ReactNode } from "react"
44
import { Loader2 } from "lucide-react"
55
import { toast } from "sonner"
66

@@ -40,6 +40,13 @@ export function FormDialog({
4040
const [pending, setPending] = useState(false)
4141
// A ref, not state: two quick Enter presses arrive before a re-render.
4242
const inFlight = useRef(false)
43+
const mounted = useRef(true)
44+
useEffect(() => {
45+
mounted.current = true
46+
return () => {
47+
mounted.current = false
48+
}
49+
}, [])
4350
const submit = async (e: React.FormEvent) => {
4451
e.preventDefault()
4552
e.stopPropagation() // dialogs portal out of, but React-bubble into, an enclosing form
@@ -53,7 +60,7 @@ export function FormDialog({
5360
toast.error(errorMessage(err))
5461
} finally {
5562
inFlight.current = false
56-
setPending(false)
63+
if (mounted.current) setPending(false)
5764
}
5865
}
5966
return (

0 commit comments

Comments
 (0)