Add preventDefault to form action
Answered
Collared Plover posted this in #help-forum
Collared PloverOP
How can I add preventDefault to my form action?
'use client'
import { useActionState } from 'react';
import InputField from "@/components/InputField";
import { login } from "./action";
export default function Login(){
const [state, formAction, pending] = useActionState(login, '');
return(
<div>
<form action={formAction}>
{state &&
<div className='error-message'>
{state}
</div>
}
<h2 className="text-lg font-medium text-secondary-100">Login to continue</h2>
<InputField
id="email"
placeholder="Email"
label="Email"
/>
<InputField
id="password"
placeholder="Password"
label="Password"
type="password"
/>
<button
className="button button-primary mt-6 w-full text-center"
type='submit'
disabled={pending}
>
Login
</button>
</form>
</div>
)
}
Answered by joulev
That one is indeed a react 19 change. But it’s not equivalent to no longer having preventDefault.
For this one I can only give you a long thread of people complaining about this and people proposing workarounds https://github.com/facebook/react/issues/29034
For this one I can only give you a long thread of people complaining about this and people proposing workarounds https://github.com/facebook/react/issues/29034
7 Replies
West African Crocodile
There is preventDefault in useActionState hook.
@joulev It has preventDefault by default. No need to add
Collared PloverOP
I thought I read online that it got removed in 15
@Collared Plover I thought I read online that it got removed in 15
No. It is always there. No need of any hook.
When hydration has not finished, the form will behave as a native html form. When hydration has finished, the form will be interactive and submitting it will not trigger a page refresh
When hydration has not finished, the form will behave as a native html form. When hydration has finished, the form will be interactive and submitting it will not trigger a page refresh
@joulev No. It is always there. No need of any hook.
When hydration has not finished, the form will behave as a native html form. When hydration has finished, the form will be interactive and submitting it will not trigger a page refresh
Collared PloverOP
The problem is that when an error occurs—like entering an incorrect password on the login page—all the input values are reset, forcing the user to re-enter everything. Is there a way to prevent this behavior?
@Collared Plover The problem is that when an error occurs—like entering an incorrect password on the login page—all the input values are reset, forcing the user to re-enter everything. Is there a way to prevent this behavior?
That one is indeed a react 19 change. But it’s not equivalent to no longer having preventDefault.
For this one I can only give you a long thread of people complaining about this and people proposing workarounds https://github.com/facebook/react/issues/29034
For this one I can only give you a long thread of people complaining about this and people proposing workarounds https://github.com/facebook/react/issues/29034
Answer