React не вимикає автозаповнення


83

Як змусити реагувати?

<input
    id={field.name}
    className="form-control"
    type="text"
    placeholder={field.name}
    autocomplete="off"
    {...field}/>

Відповіді:


158

Capital "C" autoComplete. This is mentioned in the React documentation:

https://facebook.github.io/react/docs/tags-and-attributes.html


16
Always throw an exception, not just silently ignore errors.
Yaroslav

1
@Yaroslav It's possible React throws a warning for this, if not this will github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/…
azium

1
But also, it's not error because props can be anything.. how does React know when you're making your own prop or using an html attribute?
azium

Props can't be anything for html tags.
Yaroslav

I understand why you would think that's the case, but remember these JSX tags get transpiled down to function calls with a string that represents the tag type.
azium

67

You should put:

autoComplete="new-password"

This will remove the autocomplete


13
I really don't get why this works instead of "off". Weird.
Ajay


@Pim Makes sense. Thanks!
Ajay

1
You are a legend, thanks! Also it makes sense, I wanted my new-password field to not be autocompleted :D
Erik Kubica

Probably chrome is recognising the new within the name. I named it now autoComplete="new-off" which makes a little bit more sense to me if it's not a password field and it works fine :D
kenny

7

According to Mozilla documentation, you have to set an invalid value to really turn the autocompletion off. In some browsers, autocomplete suggestions are still given even though the attribute is set to off.

This worked for me (react-bootstrap):

<FormControl
  value={this.state.value}
  autoComplete="nope"
  {...props}
/>

Wow, that's weird, but it's the only thing that's worked for me so far.
Wylliam Judd

1
@Pim Mmm is it the same autocomplete and autofill? Because it does not work for me using Chrome. It keeps autofilling the form with previous values sent with the same form.
alexventuraio

6

If you've read the correct answer and still have this issue (especially in Chrome), welcome to the club... so check how I've accomplished it:

<form autoComplete="new-password" ... >
        <input name="myInput" type="text" autoComplete="off" id="myInput" placeholder="Search field" />
</form>

Notes

  • form does not necessarily need to be the direct parent of the input element
  • input needs a name attribute
  • it also works with React-Bootstrap <FormControl/> tag (instead of <input/>)

It doesn't work. I'm trying with Chrome 67. Nothing. I still need a fake input with display: none! Very crazy!

thanks for feedback, but does your case tick all the 3 bullet notes?
w3jimmy

In some cases I was still getting autosuggestions with this. See my answer for a more complete solution.
Pim

5

None of these solutions really worked on Chrome 80.

After hours of trial and error, this very strange hack worked for me:

  1. Add autoComplete="none" to each <input> - Google skips autocomplete="off" now
  2. Wrap your fields in a container : <form> or <div>
  3. You need at least one valid input field with autoComplete="on". This should be the last element in the container. So I added the following input field to the bottom of my form:
<input 
  type="text" 
  autoComplete="on" 
  value="" 
  style={{display: 'none', opacity: 0, position: 'absolute', left: '-100000px'}} 
  readOnly={true}
/>



0

In addition to @azium's answer, <input> should be put inside the <form> tag, then autoComplete works



0

I've also tried many options, but what I ended up doing was to replace the <form> tag with <div> tag and manually manage each input that I had in that form.


0

Here's the "It works on my machine"

Chrome Version 83.0.4103.116 and React. Looks like the trick that worked for me is to put it inside of a form and add the autoComplete attribute. Notice If you try this on a non-react app, you will have to do autocomplete with a lowercase C

 <form autoComplete="off">
      <input type="text" autoComplete="new-password" />
 </form>

and

<form autoComplete="new-password">
  <input type="text" autoComplete="new-password" />
</form>

0

Yeah, most of the suggestion here and elsewhere failed in Dec 2020.
I think I tried them all, the form wrapper, setting autocomplete either to "off" of "newpassword" (neither worked), usaing onFocus, making sure I use autoComplete and not autocomplete (not capitalization) but none of them helped.

In the end mscott2005 approach worked (+1) for me but I also tweaked it for a more minimal example which I am posting as an answer for others:

No form was needed, just the two input tags:

  • autocomplete = "off" on the desired field

    <input ... autoComplete="off" />

  • autocomplete = "on" on the fake hidden field

    <input

    id="fake-hidden-input-to-stop-google-address-lookup"
    autoComplete="on"
    style={{ display: 'none'}}>
    

The id is the best I have for documenting what is really a hack without using a comment.

Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.