React v19 Discussion #11832
Replies: 20 comments 28 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. |
|
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. |
|
I am using this approach to combine React Server Actions with react-hook-form to utilize both: |
|
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>
)
} |
|
Is the team working on a golden path solution for Form Actions? |
|
Just wondering will React Compiler bring any performance boost out of box? e.g less rerenders |
|
Does anyone have a good answer for how to code a scenario where you are using |
|
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. 😅 |
|
Perhaps the experimental Form tag could be used for that: https://react-hook-form.com/docs/useform/form |
|
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. |
|
I'm still waiting for the official documentation on how to properly use RHF with the new form in React 19 |
|
React 19 with the react compiler also seems to break how |
resetForm not working in react 19 i need a solution |
|
Hey guys I wrote a blog on this check it out https://dev.to/emmanuel_xs/how-to-use-react-hook-form-with-useactionstate-hook-in-nextjs15-1hja And one thing I need to add is that I can't use useFormStatus with React-hook-form so just get the isPending value from useActionState |
|
any update on this? Looking for the best way to use |
|
Anyone came up with solution to sync the |
|
I use this simple approach on the submit button’s 'use client';
import { useForm } from 'react-hook-form';
import { useCallback } from 'react';
import { authenticate } from './actions';
type Values = { email: string; password: string };
export default function LoginForm() {
const { register, trigger, formState: { errors } } = useForm<Values>({
// default is submit-only validation
});
// Gate native submit to the server action with RHF validation
const handleValidateThenSubmit = useCallback(async (e: React.MouseEvent<HTMLButtonElement>) => {
const ok = await trigger(); // runs RHF validation now
if (!ok) e.preventDefault(); // block posting to server action if invalid
}, [trigger]);
return (
<form action={authenticate} noValidate>
<input {...register('email', { required: 'Email is required' })} name="email" type="email" />
{errors.email && <p>{errors.email.message}</p>}
<input {...register('password', { required: 'Password is required' })} name="password" type="password" />
{errors.password && <p>{errors.password.message}</p>}
<button type="submit" onClick={handleValidateThenSubmit}>
Sign in
</button>
</form>
);
} |
|
I think RHF and React 19's "use server";
import { redirect } from "next/navigation";
export type State = string | null;
export type Payload = Record<string, any>;
export const createRecord = async (payload: Payload): Promise<string> => {
try {
// do things with payload
redirect("/created");
} catch (error) {
return "some thing is wrong";
}
};The form iteself "use client";
import { useActionState } from "react";
import { useForm } from "react-hook-form";
import { createRecord, Payload, State } from "./action";
export default function page() {
const { register, handleSubmit } = useForm();
const [errString, action, isPending] = useActionState<State, Payload>(
(prevState, payload) => createRecord(payload),
null
);
return (
<div>
{errString && <h1>Error: {errString}</h1>}
<form onClick={handleSubmit(action)}>
<div>
<label htmlFor="name">User Name</label>
<input type="text" {...register("name")} />
</div>
<div>
<label htmlFor="email">User Email</label>
<input type="email" {...register("email")} required />
</div>
{isPending ? (
<button type="submit" disabled>
Submitting
</button>
) : (
<button type="submit">Submit</button>
)}
</form>
</div>
);
}and the fact that the State type is ambiguous, meaning you can shape it however you want and has huge flexibility |
|
We've been experimenting with React 19 + Our pattern is to keep
When combining it with React Hook Form, we also sync server-side validation errors back into RHF instead of duplicating validation logic. After using this approach across several projects, I extracted it into a small open-source library called zod-form-action. GitHub: https://github.com/Vish05/zod-form-action I'm the author, so this isn't meant as a neutral recommendation—I'd genuinely appreciate feedback from anyone building forms with React 19. If there are edge cases or workflows I haven't considered, I'd love to improve it. |


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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-formmay 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.
useTransitioncan 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.useFormStatusallows 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:
actionattribute, introduced in ReactDOM, impact the current strategy for handling form validation & submission by usingonSubmit={handleSubmit(myCustomSubmitHandler)}?useFormStatustake 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!
All reactions