一、简单讲解
1.When to Use Refs
There are a few good use cases for refs:
- Managing focus, text selection, or media playback.
- Triggering imperative animations.
- Integrating with third-party DOM libraries.
2.代码如下
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.textInput = React.createRef();
}
componentDidMount() {
this.textInput.current.focus();
}
render() {
return (
<input ref={this.textInput} />
);
}
}
export default App;
3.效果图,进入页面时,input框默认获得焦点
二、