setValue with shouldDirty does not work with useFormContext #9913
-
Version Number7.43.1 Codesandbox/Expo snackhttps://codesandbox.io/s/react-hook-form-v7-form-context-forked-r4r72m?file=/src/Test.js Steps to reproduce
Expected behaviour
What browsers are you seeing the problem on?No response Relevant log outputNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
This issue was raised in #72 but ignored there. |
Beta Was this translation helpful? Give feedback.
-
Please read carefully the documentation here: https://react-hook-form.com/api/useform/setvalue Method Example: https://codesandbox.io/s/react-hook-form-v7-form-context-forked-ppqznj?file=/src/Test.js |
Beta Was this translation helpful? Give feedback.
-
Actually, the problem is not form The A workaround is using Updated example: https://codesandbox.io/s/react-hook-form-v7-form-context-forked-ppqznj?file=/src/Test.js |
Beta Was this translation helpful? Give feedback.
-
I see, that makes sense. I can confirm this workaround does the job on my end as well. It feels slightly hacky, but I suppose it should be stable. Thanks for the assist. Might be worth adding a note on this to documentation, because this is non-obvious. |
Beta Was this translation helpful? Give feedback.
-
As For this Question's Answer , In form state , we use formState :{ isReady} prop to confirm the formstate subscription is completed, Use Asyncrously to validate the state. |
Beta Was this translation helpful? Give feedback.
@markedwards
Actually, the problem is not form
useFormContext
but from the order of React rendering between parent - child component.The
formState
andfieldState
is built as proxy object to avoid re-rendering, if you want the parent component to watch on the field level,setValue
must be called after proxy object is done on registering change listener of field level.A workaround is using
setTimeout
withsetValue
method insideuseEffect
of field level componentUpdated example: https://codesandbox.io/s/react-hook-form-v7-form-context-forked-ppqznj?file=/src/Test.js