share status between two routes
is there a best practice way to share a creation status prop between two pages (routes) to show a snackbar with alert message?
1 Reply
graceful-blue•4d ago
There are many ways to share state between routes. It depends on the case.
What you have described sounds like state which would come from your backend. In this case, you'd probably want to store the API response in Tanstack query and access that in whatever route you want.
If the "alert" is local, then you probably want to store it in local storage since this would presumably not be something that you expect to only receive when visiting a certain route, and would likely be consistent across usage. In this case you could use any abstraction on local storage.
If the state is temporary and doesn't exist across instances of the application, you could simply rely on a react state that is passed through a context, or any global store such as redux, zustand, tanstack store, jotai etc.
Personally, since this information probably shouldn't be in the URL and it doesn't need to exist across reloads (probably), I'd just stick it in a global store, especially since it is actually global state by the sounds of it