Show a toast after a form POST redirect #11872
Unanswered
robcaldecott
asked this question in
Q&A
Replies: 1 comment
-
After experimenting a bit more with const navigation = useNavigation();
React.useEffect(() => {
if (
navigation.state === "loading" &&
navigation.formAction?.includes("destroy")
) {
toast("User successfully deleted", { type: "success" });
}
}, [navigation]); Is this too brittle? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am following the tutorial and would like to show a toast after successfully deleting a user but I cannot figure out the best way to do this.
Currently I am using a magic
destroy
route that has a single action to remove the user and then redirect back to the home page, something like this:I then use
Form
toPOST
to the route (again, as per the tutorial)This is fine and works as per the docs but I would now like to add some form of notification (like a toast) to the user when the redirect completes.
Options I have messed with so far:
Use a URL param on the
redirect
I could do something like this:
The root route could look for this param, show the toast and then use
useSearchParams
to remove it. The problem here is the user could end up in a situation where a page refresh shows the toast again. While I am a fan of keeping state in the URL I am not sure this is a good fit.Use
useFetcher
anduseFetchers
Another idea is using
useFetcher
for the form submission and have the root route calluseFetchers
looking for a successful action completion (if that is possible), showing the toast via someuseEffect
magic.The problem here is the
/user/:id
route is getting re-rendered after the form is submitted and attempts to load a user who now no longer exists, which throws an error. What I need to do here is manually redirect to the home page after the form submission is completed but I can't see the best way to do this.I really like using a special
destroy
route coupled withForm
: I think it's a lovely pattern, but I cannot see how I can "know" when the action has completed in order to show my toast.Any advice here is welcome.
Beta Was this translation helpful? Give feedback.
All reactions