Skip to content

Commit a5b9e77

Browse files
committed
Fix useToast listener leak
1 parent f98e464 commit a5b9e77

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/hooks/use-toast.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ function toast({ ...props }: Toast) {
171171
function useToast() {
172172
const [state, setState] = React.useState<State>(memoryState)
173173

174+
// Subscribe to toast state updates once on mount. Using an empty
175+
// dependency array prevents repeatedly adding the same listener
176+
// whenever state changes, which previously caused a memory leak.
174177
React.useEffect(() => {
175178
listeners.push(setState)
176179
return () => {
@@ -179,7 +182,7 @@ function useToast() {
179182
listeners.splice(index, 1)
180183
}
181184
}
182-
}, [state])
185+
}, [])
183186

184187
return {
185188
...state,

0 commit comments

Comments
 (0)