How I fixed an issue with a React login form state and Browser autofill

⭐️ 👀 2023 WEB DEVELOPMENT BOOTCAMP starting in days! Join the waiting list to reserve your spot in my 10-weeks cohort course and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much much more! 👀 ⭐️

I stumbled upon an issue while working on a project I had a form built using React, and how browser autofill interacted with it.

You know, when the browser puts your username/password automatically because you typed it already in the past?

That’s autofill, and that’s the cause of my problem. In particular I replicated it on Chrome and Firefox, but any browser might run into this.

The form was a usual and simple form built with the useState hook.

Here’s an example email field of the form:

import { useState } from 'react'

//...

const [email, setEmail] = useState('')
<input
  id='email'
  type='email'                   
  name='email'
  value={email}
  onChange={(event) => setEmail(event.target.value)} 
/>

When you type the email in there, the email value is updated using setEmail and I’ll have it available on the form submit event, so I can send it to the server.

At some point I realized the browser was autofilling the email and password, but React didn’t recognize it!

Maybe because it fills the field before React is completely running, so it can’t possibly intercept that event.

I researched a bit and got lost into a land of browser inconsistencies and differences in how autofill works, so I had to create a simple workaround.

I did it using useRef and useEffect:

import { useState, useEffect, useRef } from 'react'

I create a ref:

const emailField = useRef(null)

and in the JSX I attach it to the input field:

<input
  ref={emailField}
  id='email'
  type='email'                   
  name='email'
  value={email}
  onChange={(event) => setEmail(event.target.value)} 
/>

Then I added a piece of code that every 100ms looks up the value of the field, and calls setEmail() to update it:

useEffect(() => {
  let interval = setInterval(() => {
    if (emailField.current) {
      setEmail(emailField.current.value)
      //do the same for all autofilled fields
      clearInterval(interval)
    }
  }, 100)
})

It’s not ideal, it involves DOM manipulation which is something we should avoid when using a library like React, but it works around this issue.

What if there’s no autofill? This will simply wait until the first character is typed, and will stop the loop.

One more thing! ⚠️ ✋

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more! 

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit bootcamp.dev