In React, with classes I can set the focus to an input when the component loads, something like this:
class Foo extends React.Component {
txt1 = null;
componentDidMount() {
this.txt1.focus();
}
render() {
return (
<input type="text"
ref={e => this.txt1 = e}/>
);
}
}
I'm trying to rewrite this component using the new hooks proposal.
I suppose I should use useEffect
instead of componentDidMount
, but how can I rewrite the focus logic?