react-hook-form: setValue with undefined resets the variable to its defaultValue

Describe the bug

  1. Using setValue('fieldName', undefined) results in setting the value to the its defaultValue instead of undefined
  2. Controller seems to ignore value change caused by setValue('fieldName', undefined)

To Reproduce Steps to reproduce the behavior:

  1. Go to Sandbox link
  2. Type anything in input -> watched value changes accordingly
  3. Click on “Set to 1234” -> Controlled input and watched value change to '1234'
  4. Click on “Set to undefined” -> Controlled input value does not change (BUG) and watched value is changed to 'default' (BUG)

Codesandbox link (Required) https://codesandbox.io/s/sharp-bird-gh7jp?file=/src/App.js

Expected behavior

  1. setValue('myField', undefined) should have set the value to undefined, and not the defaultValue 'default
  2. Controlled input should have updated its value after setValue('myField', undefined) was called

Desktop (please complete the following information):

  • OS: OSX
  • Browser: Chrome
  • Version: 87.0.4280.141

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@RLRabinowitz like I suggested this is one of the design mistakes on the current version, in which defaultValue should only appear once before the actual render. we have corrected that in the next major version: https://codesandbox.io/s/serverless-bash-txy8e