-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Formik values not updated properly when browser autofills/autocomplete #3165
Comments
Any update? |
This is an upstream issue and a duplicate. There is an issue on React's repository regarding autofill not updating controlled inputs. I'm on mobile, but if you search these issues for autofill you should see the source issue which links to the React issue. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days |
Facing same issue. |
Further reading the upstream issue facebook/react#1159 and https://bugs.chromium.org/p/chromium/issues/detail?id=1166619, it seems that Chrome is giving Javascript access to input values after user interactions. If Chrome is preventing script spies for security reasons, I don't think they're gonna simulate the As a workaround, Formik should be possible to perform a last-minute update by reading the submit event before firing What do you think? |
I posted a possible fix here -- if that problem is that autofill is occuring before React initializes. If the problem occurs on future "fill" commands using the autofill after the page has already been loaded, it's probably an unsolvable issue (or at least would require sad workarounds like manually reading uncontrolled inputs in a controlled form library). Formik is unfortunately a controlled-input library so we wouldn't likely change to use uncontrolled inputs any time soon. |
I'm with that issue and having to workaround this, it's unfortunate. |
If you don't want to use ...
<Formik ...>
{(props) => (
<AutofillSync props={props} />
...
)}
</Formik>
...
const AutofillSync = ({ props }: { props: FormikProps<FormData> }) => {
useEffect(() => {
if (
(document.querySelector('input[name="email"]') as HTMLInputElement)?.value ||
(document.querySelector('input[name="password"]') as HTMLInputElement)?.value
) {
if (!props.values.email || !props.values.password) {
props.setValues({
email: (document.querySelector('input[name="email"]') as HTMLInputElement)?.value || "",
password: (document.querySelector('input[name="password"]') as HTMLInputElement)?.value || "",
});
}
}
});
return null;
} |
@mertcanekiz I agree, but I had to add empty dependency to the useEffect: useEffect(() => {
if (
(document.querySelector('input[name="email"]') as HTMLInputElement)?.value ||
(document.querySelector('input[name="password"]') as HTMLInputElement)?.value
) {
if (!props.values.email || !props.values.password) {
props.setValues({
email: (document.querySelector('input[name="email"]') as HTMLInputElement)?.value || '',
password: (document.querySelector('input[name="password"]') as HTMLInputElement)?.value || '',
});
}
}
}, []); Else, I got in an infinite loop |
Any updates on this one? Many users use password managers these days, and having this issue opened forces developers to find alternatives, |
try: https://www.npmjs.com/package/detect-autofill add eventListener to Field component and set flag "isAutocompleted" to form state |
Looks like all these methods with
import { usePreventAutofill } from 'react-declarative';
...
const preventAutofill = usePreventAutofill();
...
return (
<InputBase
...
{...preventAutofill}
/>
);
|
Bug report
Current Behavior
Formik do not update properly values property when browser autofills user/password fields.
As you can see on this gif the input has visible values, but not internally (check the console log info on the first two renders)
Once i touch the screen (in this case opening the dev tools with F12) the onChanges are triggered and then formik updates. Also notice how the clear button is visible at end the of each input once i touch the screen.
Im using Ant Desing inputs, but the issue happens too on vanilla HTML inputs
Expected behavior
Formik should have the values updated with the browser autocomplete values
Reproducible example
Im not sure how to make the reproducible example
Your environment
The text was updated successfully, but these errors were encountered: