Better single fetch support for explicit loader/action return types #10244
Replies: 2 comments
-
deprecating json() and not exporting DataWithResponseInit means that I can't easily upgrade remix because I have to do a manual update to several hundred files instead of being able to search and replace. |
Beta Was this translation helpful? Give feedback.
-
Agreed this is making it so updating to i.e. export async function action({
request,
}: Route.ActionArgs) {
const formData = await request.formData();
const email = String(formData.get("email"));
const password = String(formData.get("password"));
const errors = {};
if (!email.includes("@")) {
errors.email = "Invalid email address";
}
if (password.length < 12) {
errors.password =
"Password should be at least 12 characters";
}
if (Object.keys(errors).length > 0) {
return data({ errors }, { status: 400 });
}
// Redirect to dashboard if validation is successful
return redirect("/dashboard");
}
export default function Signup(_: Route.ComponentProps) {
let fetcher = useFetcher();
let errors = fetcher.data?.errors; // This is typed as any
return (
<fetcher.Form method="post">
<p>
<input type="email" name="email" />
{errors?.email ? <em>{errors.email}</em> : null}
</p>
<p>
<input type="password" name="password" />
{errors?.password ? (
<em>{errors.password}</em>
) : null}
</p>
<button type="submit">Sign Up</button>
</fetcher.Form>
);
} If you do ^ with |
Beta Was this translation helpful? Give feedback.
-
We always define explicit return types, and single fetch is making this a bit tougher than it needs to be on our loaders and actions.
With
json()
deprecated in v2.14.0, we're updating our remaining actions to return plain objects where possible, anddata()
where necessary.Unfortunately this means
TypedResponse
is out, and sinceDataWithResponseInit
has not stabilized and is not re-exported by the Remix server runtime packages, for now we have to make it ourselves:An official solution would be great.
Just re-exporting
DataWithResponseInit
would be fine, but even better would be something likeSerializeTo<T>
(as a counterpart toSerializeFrom<T>
) that works whether you return a POJO ordata()
.Beta Was this translation helpful? Give feedback.
All reactions