Is there a way to subscribe with selector? e.g. omit a certain property in a watched object? #13203
-
|
Hi there I want to be as optimised as I possibly can, in the way that when I render each 'bar' data component using In my understanding, this seems not possible to do in RHF, unlike something in Zustand state management where I can do a selector with state, or do shallow subscribe. Moreover, my use case in Foo is a set of dynamic properties so I cannot statically watch 'name' and 'description', the best way is to simply omit 'bars'. If I were to do The bar component ends up subscribing to the entire parentFoo, even including itself. The alternative is to pass foo directly to each bar and omit the bar property, but on every state update inside Is my understanding correct? Has anybody attempted something like this before? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Does useWatch's const watchedValue = useWatch({
control,
name: fooPath,
compute: ({ bars, ...rest }: FormValue) => {
return rest; // This will be compared with deepEqual()
},
}); |
Beta Was this translation helpful? Give feedback.
Does useWatch's
computeproperty solve your problem? This would allow you to trigger a re-render whennameanddescriptionchange but not whenbarschanges. Note that the returnedwatchedValuewill no longer provide access tobars(though you can still usegetValues()to access it).