Skip to content

Commit

Permalink
fix: toast double update bug
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Aug 26, 2022
1 parent cce1cbb commit 8b0bd0d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-singers-kneel.md
@@ -0,0 +1,5 @@
---
"@chakra-ui/toast": patch
---

Fix issue where toast double update doesn't work
11 changes: 7 additions & 4 deletions packages/components/toast/src/toast.utils.ts
Expand Up @@ -29,10 +29,13 @@ export function findToast(toasts: ToastState, id: ToastId) {
* Given the toast manager state, finds the position of the toast that
* matches the `id`
*/
export const getToastPosition = (toasts: ToastState, id: ToastId) =>
Object.values(toasts)
.flat()
.find((toast) => toast.id === id)?.position
export function getToastPosition(toasts: ToastState, id: ToastId) {
for (const [position, values] of Object.entries(toasts)) {
if (findById(values, id)) {
return position as ToastPosition
}
}
}

/**
* Given the toast manager state, checks if a specific toast is
Expand Down
40 changes: 40 additions & 0 deletions packages/components/toast/stories/toast.stories.tsx
Expand Up @@ -425,3 +425,43 @@ export const ToastWithCustomIcon = () => {
</ButtonGroup>
)
}

export function WithDoubleUpdate() {
const toast = useToast()
const toastIdRef = React.useRef<ToastId>()

function updateOne() {
if (toastIdRef.current) {
toast.update(toastIdRef.current, { description: "1st update" })
}
}

function updateTwo() {
if (toastIdRef.current) {
toast.update(toastIdRef.current, { description: "2nd update" })
}
}

function addToast() {
toastIdRef.current = toast({
position: "bottom-right",
description: "some text",
})
}

return (
<div>
<Button onClick={addToast} type="button">
Toast
</Button>

<Button onClick={updateOne} type="button" variant="outline">
Update last toast
</Button>

<Button onClick={updateTwo} type="button" variant="outline">
Update last toast 2
</Button>
</div>
)
}

1 comment on commit 8b0bd0d

@vercel
Copy link

@vercel vercel bot commented on 8b0bd0d Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.