String event handler in React Server Components (client-side form validation in a Server Component) #48952
-
|
Hi, I've been asked a quite interesting question: in a React Server Component, in theory it should be possible to use JavaScript event handler without having to move to a Client Component. For instance, in a form: return (
<form onSubmit={(e) => {
const data = new FormData(e.target)
if (!data.get("foo)") { console.log("can't get foo") }
fetch("/api/submit", { method: "POST", body: JSON.stringify({ foo: data.get("foo") }) )
}}>This example is simplified but basically relevant for client-side validation. However, I don't completely understand why this is not possible. I suspect this is mostly a matter of serializing the function to pass it the client, am I right? Because there is no reason to involve client-side React here, so technically this function could work client-side, without needing to use a Client Component. Can we expect this to become possible in the future, in order to be able to implement form validation in RSC? Also, is there an escape-hatch that could allow us to pass a string event handler as we can do in HTML? cc @ArtyMaury |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Interleaving seems to be an interesting alternative:
|
Beta Was this translation helpful? Give feedback.
-
|
Now, NextJS has it own way of handling form validation in the server and displaying it in the client by using new React's hooks, such as |
Beta Was this translation helpful? Give feedback.
Now, NextJS has it own way of handling form validation in the server and displaying it in the client by using new React's hooks, such as
useActionState.https://nextjs.org/docs/app/guides/forms