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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…