-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toast/Messages: Fix updates incorrectly updating older version of the state #4350
Comments
In the repro, changing useToast to return the ref instead of the value fixes it. I guess this is one of the hooks rule I am always forgetting about... I'd be glad to have an explainer though. |
Let me investigate your reproducer |
OK yeah what you are doign seems way overly complex for what a Toast is needed for. You are right.... I fixed your reproducer the https://codesandbox.io/s/primereact-demo-forked-0pum4q?file=/src/App.js |
There is something inherently safer in always using setState with a function, to always use the latest version of the state. That's true, but it still fixes the issue with no additionnal overhead. Why not accept the PR?
Toasts are needed throughout the application. It makes sense to me to have a single ToastProvider. A lot of UI frameworks do that, as it removes a lot of the boilerplate code to show a Toast. |
I re-opened the issue and sent you a code review comment! |
#4349) * fix: updates incorrectly replacing old state instead of actual. * fix: follow up for #4349: apply fix to messages as well * Update Messages.js * Update Toast.js --------- Co-authored-by: Melloware <[email protected]>
Describe the bug
Without the fix, instead of being added, every update clears the state.
Before the fix:
before.mp4
After the fix:
after.mp4
Reproducer
codesandbox link
PrimeReact version
9.3.1
React version
18.x
Language
TypeScript
Build / Runtime
Next.js
Expected behavior
I don't know why it happens. I will try to investigate, but what I know for sure is that pr #4349 fixes it.
The text was updated successfully, but these errors were encountered: