I've created a very simple form using React 16.13.1, with just 2 fields:
<div>
<form onSubmit={handleSubmit}>
<div>
<label>Username</label>
<input name="username" onChange={handleInputChange} value={credentials.username} />
</div>
<div>
<label>Password</label>
<input type="password" name="password" onChange={handleInputChange} value={credentials.password} />
</div>
<div>
<input type="submit" value="Login" />
</div>
</form>
</div>
I'm using a generic function to handle the input change event:
const [credentials, setCredentials] = useState({});
const handleInputChange = event => {
const { name, value } = event.target;
setCredentials({ ...credentials, [name]: value }); // <<< This line is causing the component to re-render twice on every key typed in any field
};
Shouldn't it only re-render the component once? What is causing the second re-render?