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

Can't reset field #719

Open
benjamin-benoit opened this issue Oct 21, 2024 · 1 comment
Open

Can't reset field #719

benjamin-benoit opened this issue Oct 21, 2024 · 1 comment

Comments

@benjamin-benoit
Copy link

Describe the bug
Hello, I have an issue to reset a field on my react typescript project, I'm using zod and hook form.

To Reproduce
First I do my object:

const interventionSchema = z.object({
  thematiqueId: z.number().optional().nullable(),
  domaineId: z.number().optional().nullable(),
});

Next I do my useForm:

  const {
    register,
    handleSubmit,
    reset,
    setValue,
    watch,
    resetField,
  } = useForm<InterventionPost>({
    resolver: zodResolver(interventionSchema),
    defaultValues: {
      domaineId: undefined,
    },
  });

In my form I have on a Select:

          onValueChange={(value) => {
                        setValue("thematiqueId", Number(value));
                        reset({
                          domaineId: undefined,
                        });
                        resetField("domaineId");
                      }}

Expected behavior
On the logs, the value of domainId is not on undefined:

  useEffect(() => {
    console.log("Form Values:", watch());
  }, [watch()]);
@jorisre
Copy link
Member

jorisre commented Jan 28, 2025

Please share a reproducible example in CodeSandbox.

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

No branches or pull requests

2 participants