Redirects can't have a body, so you can't include a JSON payload in a redirect. If you want to display a message on the redirected route, you would typically use session flash.
https://remix.run/docs/en/main/utils/sessions#sessionflashkey-value
import { commitSession, getSession } from "../sessions";
export async function action({
params,
request,
}: ActionFunctionArgs) {
const session = await getSession(request.headers.get("Cookie"));
const formData = await request.formData;
const { project } = await sampleAPICall(formData);
session.flash("message", { title: "Success", message: "Yehey!" });
return redirect("`/${project?.id}`, {
headers: {
"Set-Cookie": await commitSession(session),
},
});
}
Then in your project route, you can display the message from flash
import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { getSession, commitSession } from "./sessions";
export async function loader({ request } LoaderFunctionArgs) {
const session = await getSession(request.headers.get("Cookie"));
const message = session.get("message") || null;
return json(
{ message },
{
headers: {
// only necessary with cookieSessionStorage
"Set-Cookie": await commitSession(session),
},
}
);
}
export default function Component() {
const { message } = useLoaderData<typeof loader>();
return (
<div>
{message ? (
<div className="flash">{message.title} {message.message}</div>
) : null}
</div>
);
}
react-router
libraries they also maintain, one of them being the Data APIs and routers and loaders, etc.