Below is the code:
export class Editor extends Component {
constructor(props) {
super(props);
this.state = {
name: ""
}
}
handleChange = (event) => {
event.persist();
this.setState({ name: event.target.value });
}
render() {
...
<input name="name" autoFocus={true} value={this.state.name} onChange={this.handleChange} />
<button onClick={this.handleAdd}>
Add
</button>
}
}
So the first time the component renders, the input element did have focus since I use autoFocus={true}
. But after I clicked the button, the focus didn't go back to the input but stayed on the button. My question is:
Why the input didn't regain the focus? Since onClick handler updated the component's state, which makes the component re-render again, when the component re-renders, isn't that autoFocus={true}
will re-apply as well?