-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rules of Controller does not react to changes #1749
Comments
we cache the rules object inside the controller, hence the validation rules is not changing. (so the user doesn't have to memo the rules object themself) Solutions
|
seems like my validate function was cached, too. and re-registered looks bad |
@bluebill1049 Controlled Input and Uncntrolled Input are inconsistent. Shouldn't this be fixed? https://codesandbox.io/s/react-hook-form-controller-template-oolpt |
You mean |
happy to see a simple solution for this. 🙏 basically we need to re-register the input if rules changing. however, we didn't want users to cache/memo the rules. Simplest is just to |
@bluebill1049 I think should be fix the line below. we should custom compare deps (rulesRef). https://github.com/react-hook-form/react-hook-form/blob/v5.7.2/src/controller.tsx#L110 |
yea, hopefully not going to introduce too much code for this change. I did have a note on the documentation on this too that we cache the rules. We do have |
You're right. This may not be light weight compare. |
I think we should take this as a tradeoff and documented the solution and attach that under the rules section.
🤔 an alternative solution will be on users to memo all the rules, which is not great DX. const rules1 = useMemo(() => rules, [....]])
const rules2 = useMemo(() => rules, [....]])
<Controller rules={rule1} />
<Controller rules={rule2} /> |
yes, here is the code in my project:
I have to do some explanations why I have add a useEffect here, bacause it looks werid in the “business logic” code. (I event dont really understand why unregister it can make it work, because unregister trigger something in RHF so it re-registered again ?) and another common case is, form validation, offen need some “context” information, that I have to fetch something customer specific details to determin final rules, so make rules react to the props or something similar feel more proper to me |
Thanks for your feedback. have a read on the note above first to understand the rationale behind: #1749 (comment)
Because rules are cached, so once it's
if it's coming down as props, then same use case with |
Summarise We have the following options:
3 ✅(what we choose at the moment, cheap and have work-around) |
Sorry deleted previous message... I think i found an easier solution: you can update the rules by invoking https://codesandbox.io/s/controller-rules-8pd7z?file=/src/App.tsx let me know if this helps. |
I have updated the doc and will push it alone with V6: react-hook-form/documentation@39663da |
closing this issue, documentation will be released as part of V6 update. |
Thanks 😄 |
I have a similar problem to op, "required" rule must be dynamic and on top of that i need to use setValue() on the field that was re-registered. Your last solution solves the required problemas but stops me from changing the value, i guess it changes but it is not visible on the textfield: Any ideia? |
why so complicated?
u r doing both controlled and uncontrolled, choose one. |
I changed the previous code as much as i could to simulate my real problem. As you can see, required erros appear even though the fields are not required in that moment. |
Taking a quick glance looks like the wrong usage, why are you set values in there
personally I would build an entire component that includes all those fields and wrap with the controller. when you using controller and then manually use setValue it raise concern to me right away. |
is this codesandbox works for you? https://codesandbox.io/s/controller-rules-8pd7z?file=/src/App.tsx @pedroguia if so, maybe try to follow the same pattern. |
Cant build a component that includes all of these fields because they are already in different bigger components inside my project. I've already tried that approach and coudnt make it work, bu ill try again. |
ok, I will take a look closer during lunch time. |
Ops! Validate do not work with values of useState, and |
do you have a codesandbox for this? |
if anyone's situation is simply depend on a prop, and it change only once, maybe use
|
@acfasj we are fixing this next release. |
I also have this problem due to a recent upgrade from 5.3.x to 5.7.x Is it possible to add flag Rules work fine without Rules work differently with |
we are fixing the in v6, no extra props required. |
This is exactly my issue as well. Values of useState are not respected by Validate. Will this be what is addressed in v6 @bluebill1049? |
Yes, correct. right now you have to follow what i did above for a work around. |
This? Okay, thanks @bluebill1049 ! I have the luxury of waiting for the release, so I'll do that for now! |
we will have another RC version before the final release of v6. (make sure the quality is good) |
unregister input at useEffect and let Controller re-register itself with updated rules. I have a question, does that mean I don't have to re-register manually, the Controller will re-register automatically. And in my opinion, re-register is a hack way of writing that does break the existing internal state of a control, such as dirty, touch, etc |
V6 will update |
First, the lib is very cool!! Thanks!
My problem is in set the rule in Controller:
The property rules of Controller does not not react to
needRegister
Codesandbox link (Required)
Expected behavior
React to changes in rules
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: