question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setValue() does not trigger a update to the watch when used in useFormContext

See original GitHub issue

Describe the bug A clear and concise description of what the bug is. I am currently trying to listen to changes watch and in the scenario where I use the useFormContext() hook, the watch doesn’t update onChange. Avoiding the use of refs given I am currently hooked in materialUI

To Reproduce Steps to reproduce the behavior:

  1. Set up a function component with a useForm hook ’
  2. render a <FormContext {...methods} >
  3. Create function component named ‘Input’ that is an input that is state controlled.
  4. In the above component hook up via the useFormContext() Example:

`import React, { useEffect } from “react”; import { Box, Typography, FormControl, TextField } from “@material-ui/core”; import { useFormContext } from “react-hook-form”;

type SomeFakeInputProps = { name: string; require?: boolean; fieldName: string; defaultValue: string; };

export const SomeFakeInput: React.FC<SomeFakeInputProps> = ({ fieldName, name }) => { const [value, setStateValue] = React.useState(“”); const { register, errors, formState, setValue } = useFormContext(); const fieldTouched = formState.touched.includes(fieldName); const error = errors[fieldName];

useEffect(() => {
    register({ name: fieldName });
}, []);

function handleChange({ target }: React.ChangeEvent<HTMLInputElement>) {
    setValue(fieldName, target.value);
    setStateValue(target.value);
}
return (
    <Box>
        <FormControl disabled={formState.isSubmitting}>
            <TextField aria-label={name} name={fieldName} onChange={handleChange} />
            {fieldTouched && error && <Typography color="error"> {error} </Typography>}
        </FormControl>
    </Box>
);

};`

  1. Log out the watch from the origin useForm() hook and notice that watch state is not being updated on input change.

I will try to make a code sandbox to replicate this issue. Current don’t have the capacity to do so.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewlsimplisafecommented, Sep 19, 2019

Ahh awesome! Thanks for the quick response. I managed to build out a quick demo of the issue.

https://codesandbox.io/s/react-hook-form-setvalue-with-controlled-input-bug-fgeik

0reactions
bluebill1049commented, Sep 19, 2019

https://github.com/react-hook-form/react-hook-form/releases/tag/v3.23.10 published the fix thanks 😃 please star the repo to support if you find it useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useForm - setValue - React Hook Form
Only the following conditions will trigger a re-render: When an error is triggered or corrected by a value update. When setValue cause state...
Read more >
watch not working when setValue sets nested objects
I pass a complete object to setValue and the input box is updated correctly, but my watch is not printing:
Read more >
React-Hook-Form Library | Common Use Cases
By default, when you type something in the input field, the react-hook-form doesn't trigger any re-render, but by calling the watch function, we...
Read more >
Using Material UI with React Hook Form - LogRocket Blog
That's why inside the render function we are not using the field prop anymore. To set the value we are taking another new...
Read more >
react hook form onblur not working - You.com | The AI Search ...
no idea), in certain conditions onChange() doesn't trigger when pasting text with CTRL+V . Luckily, the regular onInput() event still triggers, so we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found