Last active
January 9, 2025 14:47
-
-
Save teknosains/51828c801a78709e42d3f76f113c1647 to your computer and use it in GitHub Desktop.
Showing and Dismissing loading Toast in clientAction function in Remix / React Router
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cara simple Showing and Dismissing loading Toast | |
// di clientAction function in Remix / React Router | |
// Toast library: https://react-hot-toast.com/docs/toast | |
function runFormValidation(formData) { | |
//... | |
} | |
export async function action({ request, params }: ActionFunctionArgs) { | |
const formData = await request.formData(); | |
// some server-side form validation maybe | |
runFormValidation(formData); | |
return json({ somdata }) | |
} | |
export async function clientAction({ request, serverAction}: ClientActionFunctionArgs) { | |
const formData = await request.clone().formData(); | |
// some client-side form validation maybe | |
runFormValidation(formData); | |
// start loading after form validation passed | |
toast.loading('Processing...' , { | |
duration: Infinity, | |
}); | |
return await serverAction<typeof action>().finally(() => { | |
// dismiss when the server-side action finish | |
toast.dismiss(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment