Skip to content

setValue with shouldDirty does not work with useFormContext #9913

Answered by leapful
markedwards asked this question in Q&A
Discussion options

You must be logged in to vote

@markedwards

Actually, the problem is not form useFormContext but from the order of React rendering between parent - child component.

The formState and fieldState 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 with setValue method inside useEffect of field level component

Updated example: https://codesandbox.io/s/react-hook-form-v7-form-context-forked-ppqznj?file=/src/Test.js

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@markedwards
Comment options

@markedwards
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by markedwards
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #9912 on February 10, 2023 13:30.