Last active
November 22, 2023 22:40
-
-
Save diboune/cc9d3d42081df6b30c7203ac6e844038 to your computer and use it in GitHub Desktop.
Fetcher Error Handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useFetcher } from "@remix-run/react"; | |
import { Control, Field, Message, Root, Submit } from "@radix-ui/react-form"; | |
import { useEffect, useState } from "react"; | |
import { type DataFunctionArgs } from "@remix-run/node"; | |
export default function Testing() { | |
const { Form, data } = useFetcher<typeof action>(); | |
const [error, setError] = useState(false); | |
useEffect(() => { | |
if (data?.error) { | |
setError(data?.error); | |
} | |
}, [data]); | |
return ( | |
<Root asChild> | |
<Form | |
method="POST" | |
> | |
<Field serverInvalid={error} name="email"> | |
<Control asChild> | |
<input | |
onChange={() => { | |
setError(false); | |
}} | |
className="invalid:border-red-900 text-black border-2 box-border w-full bg-blackA2 shadow-blackA6 inline-flex h-[35px] appearance-none items-center justify-center rounded-[4px] px-[10px] text-[15px] leading-none shadow-[0_0_0_1px] outline-none" | |
type="email" | |
required | |
/> | |
</Control> | |
<Message match="typeMismatch" forceMatch={error}> | |
Please provide a valid email. | |
</Message> | |
</Field> | |
<Submit>Submit</Submit> | |
</Form> | |
</Root> | |
); | |
} | |
export async function action({ request }: DataFunctionArgs) { | |
const form = await request.formData(); | |
const email = form.get("email"); | |
if (email === "[email protected]") { | |
return { error: true }; | |
} | |
return { error: false }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment