Skip to content
Discussion options

You must be logged in to vote

What’s happening is that setValue runs before the Controller for unit_value has registered the field on the first render, so React Hook Form doesn’t know yet that it should update that input. That’s why you see the correct value in the console, but nothing shows up in the field until one of the dependencies changes and triggers a re-render.

You can fix this by making sure the field is registered before you call setValue. The easiest way is to add shouldUnregister: false in your useForm setup, or you can force an update once everything is mounted using something like:

useEffect(() => {
  if (sales && net_receipts) {
    const unitValue =
      sales !== "" && sales !== "0" && net_receipts …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@K3zter
Comment options

Answer selected by K3zter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants