Next.js Discord

Discord Forum

state is undefined in useFormState with server actions

Unanswered
Peterbald posted this in #help-forum
Open in Discord
PeterbaldOP
basically I have a client component like this

'use client';
import { useFormState } from "react-dom";

export default Form({ action, children }: SomeTypeHere) {
  const [state, formAction] = useFormState(action, { message: '' });
  console.log(state);
  return (
    <form action={formAction}>
      {children}
    </form>
  );
}


and I’m using <Form action={…}>…</Form> in some other server components

There are no errors anywhere whatsoever, I tested with a console.log in the server action before the return { … } statement and it does log properly, but despite returning a valid message string the state shows up as undefined in the browser console (??) (the initial state logs properly, it’s just that when I submit the form the state becomes undefined)

Another behaviour is that the state only updates to undefined once… if I press submit multiple times it doesn’t log ever again. Not sure if this helps?

4 Replies

PeterbaldOP
help?
PeterbaldOP
and it doesn't work
Giant panda
I have the same issue, It was working a while ago but it just stopped suddenly