To use react-toastify for client-side rendering, import the toast styles in your links function, include the ToastContainer in your component tree, and trigger notifications inside a useEffect hook using the toast object from your loader data.
import { data, type LinksFunction, type LoaderFunctionArgs } from "react-router";
import { Links, LiveReload, Meta, Outlet, Scripts, ScrollRestoration, useLoaderData } from "react-router";
import { useEffect } from "react";
import { getToast } from "remix-toast";
import { ToastContainer, toast as notify } from "react-toastify";
import toastStyles from "react-toastify/ReactToastify.css?url";
// Add the toast stylesheet
export const links: LinksFunction = () => [{ rel: "stylesheet", href: toastStyles }];
export const loader = async ({ request }: LoaderFunctionArgs) => {
const { toast, headers } = await getToast(request);
return data({ toast }, { headers });
};
export default function App() {
const { toast } = useLoaderData<typeof loader>();
// Hook to show the toasts
useEffect(() => {
if (toast) {
// notify on a toast message
notify(toast.message, { type: toast.type });
}
}, [toast]);
return (
<html lang="en">
<head>...</head>
<body
>
...
{/* Add the toast container */}
<ToastContainer />
</body>
</html>
);
}