Loading...
async function signup(formData) {
const email = formData.get('email');
return await createUser(email);
}
const [state, formAction, isPending] = useActionState(signup, null);async function signup(prevState, formData) {
const email = formData.get('email');
return await createUser(email);
}
const [state, formAction, isPending] = useActionState(signup, null);The action receives the previous state first and the form data second, so formData.get() works on the right argument.
formData from the first argument, which is actually the previous state.useActionState is imported from 'react', not the old useFormState from 'react-dom'.(prevState, formData), with the previous state as the first argument.isPending drives the button state instead of a separate useState flag.They are the same idea. useFormState from 'react-dom' was renamed to useActionState and moved to 'react'; the old name is deprecated.
Yes. Call the returned dispatch function inside startTransition, for example from a button click handler.
Yes. Pass a function marked with 'use server' as the action, and the returned state comes back from the server.