Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value prop on field doesn't update #1154

Open
seththoburn-cc opened this issue Feb 17, 2025 · 1 comment
Open

Default value prop on field doesn't update #1154

seththoburn-cc opened this issue Feb 17, 2025 · 1 comment

Comments

@seththoburn-cc
Copy link

Describe the bug

When using the defaultValue prop on form field, if the value is updated, the default doesn't update when the prop is updated.

I could understand maybe if this was intentional behavior, but this isn't how the defaultValues prop on the form hook works, those can be updated.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-r858qffw?file=src%2Findex.tsx

Steps to reproduce

Open the reproduction above. Notice the setTimeout in the useEffect. The default value does not update to "Updated" after one second.

Expected behavior

The form field value should update to "Updated" after one second.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Mac
  • Browser: Arc (Chromium)
  • Version: 133.0.6943.54 (Official Build) (arm64)

TanStack Form adapter

react-form

TanStack Form version

0.42.0

TypeScript version

5.1.0

Additional context

I would like to use the default value feature to derive the value of a field from the value of others, but this makes it impossible to do so.

@seththoburn-cc
Copy link
Author

Workaround if anyone runs into this same issue, set the field value in a useEffect (or you can do it where you calculate the computed value) with "dontUpdateMeta" set to true. This is how it appears the default value is normally set internally.

  useEffect(() => {
    form.setFieldValue('firstName', defaultValue, {
      dontUpdateMeta: true,
    });
  }, [defaultValue, form]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant