Open jluxenberg opened 1 year ago
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
react-hot-toast | ❌ Failed (Inspect) | May 4, 2023 4:48pm |
I am also experiencing this bug. toast called inside useEffect
do not show. or show sometime later which is not desired
@timolins Any chance of getting this merged? I just merged main
into it so should be good to go. Let me know if there's anything I can do to help!
Hey, has anything changed with getting this fixed or merged into main. This problem still seems to be hanging around. Thanks
Issues
toast.*
function from within a useEffect hook, the toast does not appear (see the new test case in toast.test.tsx for an example).<Toaster>
component, then rendering the<Toaster>
component, would cause the toast to not appear or to appear "stacked up" on top of each other.Cause
The
useStore
hook (used byToaster
) calls "useState" to get the current state of the globalmemoryState
, and then calls "useEffect" to set up a subscription to the globalmemoryState
.The bug is caused when a change to the global
memoryState
happens after "useState" is called, but before the "useEffect" callback is run. This can happen e.g. if we call a toast.* function from within another component's useEffect hook.Solution
Use the
useSyncExternalStore
hook instead ofuseState
anduseEffect
; it handles these cases correctly.I've added a dependency on the
use-sync-external-store
shim so that React version below 18 are still supported.Related Github issues
https://github.com/timolins/react-hot-toast/issues/101 https://github.com/timolins/react-hot-toast/issues/57 https://github.com/timolins/react-hot-toast/issues/142 https://github.com/timolins/react-hot-toast/issues/253