Skip to content
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

issue: isValid changes from false to true on first render when react strictMode enabled #9765

Closed
1 task done
AlexanderKositsyn opened this issue Jan 12, 2023 · 12 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@AlexanderKositsyn
Copy link

AlexanderKositsyn commented Jan 12, 2023

Version Number

7.40.0

Codesandbox/Expo snack

https://codesandbox.io/s/react-hook-form-v7-controller-ts-forked-8ikori?file=/src/index.tsx

Steps to reproduce

  1. Go to console
  2. See isValid changes from false to true

Expected behaviour

isValid is false, because input is required
image

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@AlexanderKositsyn
Copy link
Author

@Moshyfawn
Copy link
Member

formState is initiated with isValid: false by default (we can't know the "correct" value until both the hook and the components are rendered in that order). As you're using a field level validation (Controller rules), form needs to "evaluate" the inputs in the form and their "rules" and set the correct isValid value on next re-render

@AlexanderKositsyn
Copy link
Author

You can remove React.StrictMode component and isValid stays false
https://codesandbox.io/s/react-hook-form-v7-controller-ts-forked-8ikori?file=/src/index.tsx

@AlexanderKositsyn
Copy link
Author

AlexanderKositsyn commented Jan 12, 2023

@Moshyfawn is isValid changing from false to true expected behaviour?

@Moshyfawn
Copy link
Member

@Moshyfawn is isValid changing from false to true expected behaviour?

My bad, I read the true & false values wrong. Let's see..

@Moshyfawn
Copy link
Member

@bluebill1049 it seems like whatever is causing it is fixed in origin/master and was related to this executeBuiltInValidation call.

You can checkout this codespace: navigate to Basic onChange test and see that isValid is false

@bluebill1049
Copy link
Member

thanks a lot @Moshyfawn I will release the minor update today.

@bluebill1049 bluebill1049 added the status: working in progress working in progress label Jan 12, 2023
@bluebill1049 bluebill1049 removed the status: working in progress working in progress label Jan 13, 2023
@AlexanderKositsyn
Copy link
Author

@bluebill1049 bluebill1049 reopened this Jan 13, 2023
@bluebill1049 bluebill1049 added status: under investigation aware of this issue and pending for investigation bug Something isn't working labels Jan 13, 2023
@leapful
Copy link
Contributor

leapful commented Jan 13, 2023

@bluebill1049

It seems the root cause is from line 97 in useController.ts when useEffect hook performs clean up callback twice in React.StrictMode that causes setting flag mount to false.

React.useEffect(() => {
const updateMounted = (name: InternalFieldName, value: boolean) => {
const field: Field = get(control._fields, name);
if (field) {
field._f.mount = value;
}
};
updateMounted(name, true);
return () => {
const _shouldUnregisterField =
control._options.shouldUnregister || shouldUnregister;
(
isArrayField
? _shouldUnregisterField && !control._stateFlags.action
: _shouldUnregisterField
)
? control.unregister(name)
: updateMounted(name, false);
};
}, [name, control, isArrayField, shouldUnregister]);

With mount=false, validation will be stopped and always return valid state.

if (!mount || disabled) {
return {};

@bluebill1049
Copy link
Member

bluebill1049 commented Jan 13, 2023

yes @leapful you are right. I get deep-fried by this useEffect twice in strict mode. Try to figure out a solution to this.

@bluebill1049
Copy link
Member

thanks, @leapful for your help! Finally, figure out a solution for this.

@bluebill1049 bluebill1049 removed the status: under investigation aware of this issue and pending for investigation label Jan 13, 2023
@AlexanderKositsyn
Copy link
Author

@bluebill1049 Thanks!
When it will be released?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants