[Partially solved] SSR/NextJS: "A component is changing an uncontrolled input to be controlled ..."
Hi all,
I just followed this guide to get set up with TanStack Form + NextJS: https://tanstack.com/form/latest/docs/framework/react/guides/ssr
I have been careful to pour over the code multiple times, and I believe I'm doing everything that the tutorial is doing, besides client-side validation (for the moment), and yet I still get the error/warning in the console that an input field is changing from uncontrolled to controlled. I've added logging to see that, indeed, the initial value of the inputs is undefined before being updated to empty string.
This is my code, in the same order as the guide:
form options
server action
client component
One fix is to set the
defaultValue
prop on each field, but I feel like I should not need to do this given the default value is well defined in the form option object.
I'm not sure what I've done wrong here, as after reading the guide again a few times, I've not noticed any discrepancy.
How are you guys using TanStack Form w/ SSR and avoiding this problem? Or did I miss something?
Thank you in advance.3 Replies
flat-fuchsiaOP•9mo ago
I've seen this as well: https://tanstack.com/form/latest/docs/framework/react/guides/debugging
But I haven't forgotten the key in my useForm hook, so I don't think it is applicable.
Debugging React Usage | TanStack Form React Docs
Here's a list of common errors you might see in the console and how to fix them. Changing an uncontrolled input to be controlled If you see this error in the console: Warning: A component is changing...
flat-fuchsiaOP•9mo ago
I've tried messing with the useActionState call because I had a suspicion the initialFormState value (that initializes the initial form state to undefined) had something to do with this, but I couldn't figure out a way to get that to work. I have no clue what to do about this, and at this point it's becoming a fairly massive blocker so I think I'm going to have to move on from this library for now.
Nevermind. One last hail mary attempt to fix was changing this:
to this:
and it worked.
The guide has us use
initialFormState
, defined as:
And, so, yes, obviously the form has values starting as undefined. There was no way to change that based on the guide, so I figured I could just write my own "initial form state" where the values are explicitly the ones I defined before. This seems messy, and perhaps not what the library author intended, so I don't know the better way. But this works for now. I'll probably just write a helper to create the initial form state with specified values.extended-salmon•9mo ago
CC @Leonardo who's working on our SSR story