-
-
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
formState.isValid return true
in initial render with mode onChange,onBlur
#1512
Comments
This is expected behavior, react hook form is embraced uncontrolled input. the validation can only be assured after render. |
I understand when validation is executed but with onBlur and onChange mode he start with state isValid= |
why would you need to do that? it's the second render, this is not going to impact user experience right? |
User experience... no. But if I have some conditional rendering like |
yes @polovi that's a challenge to solve, if you rewind back to v3 we had false to start with. if we start as false, then as we render all inputs input 1. pass -> render true you see multiple re-render will have to decide the form is vald or not, that's the reason why we start with true and re-render only once. |
And what if will be possible to enforce initial validation with some useForm() option. So when we know that form is really invalid on start it could skip validation like with mode onSubmit. |
it's already optimized to skip with onSubmit. |
i'm having the same needs for the |
Hi @bluebill1049, love the library. I was bit by this issue pretty hard today. I'm using RHF in React Native with a form that doesn't have a submit button. So I use watch + a useEffect hook to watch formState.isValid. Based on isValid, I determine when updates should be made to the model. But since isValid gives a false positive, I have to put in a sketchy check. I need to always trust isValid. I understand why you can't know on initial render, but I need to be able to account for this part of the lifecycle. Perhaps a formState.isInitializing or something that would let me know what RHF is doing during this phase. Using @latest and onChange BTW. |
how about keeping a single useRef at your form level?
|
Thanks, I think my UI was a bit too clever, so I went a different route
with a submit button and all is good now.
…On Wed, Sep 23, 2020 at 4:41 PM Bill ***@***.***> wrote:
Hi @bluebill1049 <https://github.com/bluebill1049>, love the library. I
was bit by this issue pretty hard today. I'm using RHF in React Native with
a form that doesn't have a submit button. So I use watch + a useEffect hook
to watch formState.isValid. Based on isValid, I determine when updates
should be made to the model. But since isValid gives a false positive, I
have to put in a sketchy check. I need to always trust isValid. I
understand why you can't know on initial render, but I need to be able to
account for this part of the lifecycle. Perhaps a formState.isInitializing
or something that would let me know what RHF is doing during this phase.
Using @latest <https://github.com/latest> and onChange BTW.
how about keeping a single useRef at your form level?
const isInital = useRef(true)
useEffect(() => isInital.current = false, [])
// isInital.current use this
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1512 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AARM2IVYVS4MXWGHOZRTIADSHKBTPANCNFSM4MR24MYA>
.
|
Is your feature request related to a problem? Please describe.
With onSubmit mode isValid initially return
false
but with onBlur, onChange it istrue
before validation is executedhttps://codesandbox.io/s/flamboyant-lake-limo5?file=/src/App.js
Describe the solution you'd like
The form should be invalid until validation is executed
The text was updated successfully, but these errors were encountered: