I built a controlled form component with hooks and used styled-components instead of a styles.css file. However every time I did a key press in an input field, focus was lost. After much investigation, I discovered that by reverting back to a styles.css file and abandoning styled-components, everything worked as expected. Is this a known problem with styled-components when using hooks?
UPDATED 4/7/2019
Here is example code. The first field has "input" changed to "Input" using the styled component. Focus is lost in that field on each click. The second field has no problem. Of course the form is rendered each time setFirstName() is called, and the focus is lost as is explained in many other StackOverflow answers. But renders after each click does not effect the focus in the second field. Why can't the same be true for the field using a styled-component?
import React, { useState } from "react"
import styled from "styled-components"
import "./styles.css"
function Form() {
console.log("Form rendered")
const [firstName, setFirstName] = useState("")
const [lastName, setLastName] = useState("")
const Input = styled.input`
border: 2px solid blue;
margin-right: 15px;
width: 124px;
font-weight: 800;
`
return (
<form>
<Input
value={firstName}
onChange={e => setFirstName(e.target.value)}
placeholder="First name"
type="text"
name="firstName"
/>
<input
value={lastName}
onChange={e => setLastName(e.target.value)}
placeholder="Last name"
type="text"
name="lastName"
/>
</form>
);
}
export default Form
I ran this code in CodeSandbox, but that was my first time there (learning it) and I don't know how to make it available to others.