-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Type of loaderData wrong when returning redirect in the loader (action as well) #12335
Comments
Hi, I had a similar issue. If you throw the redirect instead of returning it, the typescript error should go away. |
Facing the same problem - just migrated from remix to rr7 and the inferred return types for actions/loaders are messed up. I'm returning a |
Since |
Confused as to why this did work in Remix though? |
Good point, not sure if there was something we should be carrying over from Remix c.c. @pcattori |
Also, if I need to redirect the user after logging in, I do something like this in my action return createUserSession({
request,
userId: user.id,
email,
roles,
remember: true,
redirectTo: redirectTo ?? "/app",
isAdmin: user.isAdmin,
}) where async function createUserSession({
request,
userId,
remember,
redirectTo,
roles,
email,
isAdmin,
}: {
...
}) {
const { session } = await getSession(request)
...
session.set(IS_ADMIN, isAdmin)
return redirect(redirectTo, {
headers: {
"Set-Cookie": await commitSession(session, {
maxAge: remember
? 60 * 60 * 24 * 2 // 2 days
: undefined,
}),
},
})
}
|
The fix is to exclude |
I had the same issue. In Remix 2.x, returning a redirect somehow worked and didn't mess with the loader / action types. Having to " throw " a successful authentication request seems a bit weird to me. And it introduces a strange behavior in my app, I need to fully reload before showing the authenticated user info. |
Is it the recommended practice to throw |
same here, i end up with import { redirect } from 'react-router';
import type { Route } from './+types/blog';
type BlogType = {
userId: number;
id: number;
title: string;
body: string;
};
export async function loader({ params }: Route.LoaderArgs) {
const data = await fetch(
`https://jsonplaceholder.typicode.com/posts/${params.id}`
);
if (!data) {
redirect('/');
return; // return undefined as default
}
return (await data.json()) as BlogType;
}
export default function Blog({
params,
loaderData,
}: Route.LoaderArgs & Route.ComponentProps) {
// check if loaderData is undefined
if (!loaderData) {
return null;
}
return (
<div>
<h1>blog id: {params?.id}</h1>
<p>{loaderData.title}</p>
<p>{loaderData.body}</p>
</div>
);
} edit: |
If you throw a redirect instead of returning in an action or loader when using The workaround I have found is to assert the return as never
|
🤖 Hello there, We just published version Thanks! |
🤖 Hello there, We just published version Thanks! |
What version of React Router are you using?
7
Steps to Reproduce
Expected Behavior
some
should be defined on the loaderDataActual Behavior
there is a typescript error
The text was updated successfully, but these errors were encountered: