React v19 Discussion #11832
Replies: 12 comments 15 replies
-
I too am wondering about your first question. In general the hooks/features offered around actions don't seem to align well with client side validations. Although I know you can just call an action directly in your own submit handler. Still, would be nice to use the action prop on the form API. |
Beta Was this translation helpful? Give feedback.
-
One benefit of the new One solution I've seen that makes use of both the
The code looks something like this: import { serverAction } from "./actions"
const form = useForm()
const formRef = useRef(null)
const [state, formAction] = useActionState(serverAction, { message: null })
return (
<Form {...form}>
<form
ref={formRef}
action={formAction}
onSubmit={form.handleSubmit(() => formRef.current?.submit())}
>
{/* form fields here */}
</form>
{state.message != null && <p>state.message</p>}
</Form>
) Functionally I think this is great, since it lets us take advantage of progressive enhancement and the new React 19 features, but there is a bit too much going on here I think, making it feel a little clunky. It would be nice to consolidate all of this behavior into one, but it might not align well with the vision of this specific library. In that case, the workaround above^ seem to work fine for now. |
Beta Was this translation helpful? Give feedback.
-
One issue that I see and don't know how to fix in Next.js is the following error: |
Beta Was this translation helpful? Give feedback.
-
I am using this approach to combine React Server Actions with react-hook-form to utilize both:
|
Beta Was this translation helpful? Give feedback.
-
Maybe you don't need onSubmit for your case. export function CreateCategoryForm() {
const [state, formAction] = useActionState(createCategoryAction, null)
const {
register,
setValue,
formState: { errors, isValid },
} = useForm<CreateCategory>({
mode: 'onChange',
resolver: zodResolver(CreateCategorySchema),
})
return (
<Form action={formAction}>
<Fieldset legend="Content">
<TextInput
label="Title"
message={errors.title?.message}
{...register('title')}
/>
<TextArea
label="Description"
message={errors.description?.message}
{...register('description')}
/>
</Fieldset>
<Fieldset legend="Actions">
<input type="submit" value="Create" disabled={!isValid} />
</Fieldset>
</Form>
)
} |
Beta Was this translation helpful? Give feedback.
-
Is the team working on a golden path solution for Form Actions? |
Beta Was this translation helpful? Give feedback.
-
Just wondering will React Compiler bring any performance boost out of box? e.g less rerenders |
Beta Was this translation helpful? Give feedback.
-
Does anyone have a good answer for how to code a scenario where you are using |
Beta Was this translation helpful? Give feedback.
-
What would be wonderful would be a new hypothetical helper function const {
register,
registerAction,
formState: { errors },
} = useForm<FormValues>()
return (
<form
action={registerAction(action)}
>
{/* form fields here */}
</form>
) No idea if that's feasible. 😅 |
Beta Was this translation helpful? Give feedback.
-
Perhaps the experimental Form tag could be used for that: https://react-hook-form.com/docs/useform/form |
Beta Was this translation helpful? Give feedback.
-
In my case, triggering The server action got called but I'm guessing it's more like a Didn't spot the problem first because it looks like everything works because of the "progressive enhancement", i.e. works without javascript enabled. // This was triggering a full page reload for me
onSubmit={form.handleSubmit(() => formRef.current?.submit())} Instead I tried to just validate in action={formAction}
onSubmit={(event) => {
const isValid = checkFormValidation(); // Can't call "trigger" here and await the result before calling "preventDefault"
if (!isValid) {
event.preventDefault();
}
}} For me, it feels like you had to do too many special things to make RHF work with the new React 19 form and server actions. |
Beta Was this translation helpful? Give feedback.
-
I'm still waiting for the official documentation on how to properly use RHF with the new form in React 19 |
Beta Was this translation helpful? Give feedback.
-
Hey,
Some exciting new API's and Hooks are being teased as part of the React v19 upgrade. There is a lot more thought going into how the core React API can help with form submission, and some of their new hooks may be worth looking into sooner rather than later to perhaps identify how
react-hook-form
may or may not be able to integrate with it (or whether it should).So, what's coming up in React 19?
Actions
TL;DR lots of thought around how taking data from a form and making an API request to save the data could be simplified/improved.
useTransition
can be used to save data from a basic formuseActionState
- accept an "action" argument and returns any errors, the action itself, and whether the action is stillpending
. If anyone saw the canary release, it was previously nameduseFormState
.useFormStatus
allows access to the state of a form for any child component of the HTML form, without need for additional context or providers.useOptimistic
- This doesn't look specific to forms, but nonetheless is a feature to make displaying optimistic responses from pending API requests a little easier.Feel free to contribute to the discussion if you have any thoughts or ideas. I've been thinking about it myself since I read the announcement, and i've already got some high level questions:
action
attribute, introduced in ReactDOM, impact the current strategy for handling form validation & submission by usingonSubmit={handleSubmit(myCustomSubmitHandler)}
?useFormStatus
take responsibility of some of the state that react-hook-form currently handles? It will initially expose four properties; pending, form data, form method and the action itself.All high level questions and open to scrutiny / debate, Though in general it is quite exciting to see the React core team focus efforts on forms, something that I have always seen as being particularly neglected, and largely why I enjoy working with react-hook-form for managing forms!
Beta Was this translation helpful? Give feedback.
All reactions