Replies: 5 comments 5 replies
-
In React Router v7 there is a new When no custom status codes or headers are needed, raw objects can be returned from loaders/actions. |
Beta Was this translation helpful? Give feedback.
-
There’s also |
Beta Was this translation helpful? Give feedback.
-
So how can I return a loader response without using |
Beta Was this translation helpful? Give feedback.
-
Hey there, What am i doing wrong? I've put together a relative simple demo example: export async function action({ request }: Route.ActionArgs) {
const body = await request.json();
const resp = await AuthService.postLogin({
body: {
email: body.email,
password: body.password,
},
});
if (resp.response.ok) {
throw redirect("/");
}
return Response.json(resp); // or returning the raw object, or using data()
}
export default function Home({ actionData }: Route.ComponentProps) {
const submit = useSubmit();
return (
<>
<Form
onSubmit={(data) => {
submit(data, { method: "POST", encType: "application/json" });
}}
>
<TextInput name="email" label="Email" type="email" />
<TextInput name="password" label="Password" type="password" />
<button type="submit">Submit</button>
</Form>
{actionData && <p className="text-red-500">{actionData.error.message}</p>}
</>
);
} |
Beta Was this translation helpful? Give feedback.
-
Hi there! Checking this commit and reading what @ingmarh and @xeroneon said, I was able to replace the Example: export const loader = async ({ request }: LoaderFunctionArgs) => {
const session = await auth.api.getSession()
const myCustomType: MyCustomType = {
isLoggedIn: true,
token: session.token,
};
return data( myCustomType, { status: 200, headers: session.headers })
} and then using the const actionData = useActionData<MyCustomType>();
console.log(actionData.isLoggedIn);
console.log(actionData.token); I hope this helps someone who comes across the same problem and finds this thread. |
Beta Was this translation helpful? Give feedback.
-
Hi!
When using future.v7_skipActionErrorRevalidation the json() method has the nice utility of altering response status to 4xx/5xx without the need to explicitly create the response yourself (providing the ability to opt-out of revalidation in case of an error submitting some data etc.).
It also provides a convenient way to require returned data to conform to a given type.
The alternatives i see for typing is probably using "satisfies".
But with that and the revalidation opt-out i see myself recreating the json() method myself, and therefore would be interested to hear the reason why it is beeing deprecated.
Beta Was this translation helpful? Give feedback.
All reactions