168
rated 0 times
[
172]
[
4]
/ answers: 1 / hits: 18851
/ 6 Years ago, wed, november 7, 2018, 12:00:00
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?
More From » reactjs