Ref String(最不被推荐的一种方式,即将被废弃)
class test extends Component {
render() {
return (
<div>
<span ref="StringRef">content</span>
</div>
);
}
componentDidMount() {
this.refs.StringRef.textContent = 'String ref got'
}
}
Ref Function
class test extends Component {
render() {
return (
<div>
<span ref={ele => {this.methodRef=ele}}>content</span>
</div>
);
}
componentDidMount() {
this.methodRef.textContent = 'method ref got'
}
}
React.createRef()
class test extends Component {
constructor() {
super();
this.objRef = React.createRef();
//objRef = {current:null}
}
render() {
return (
<div>
<span ref={this.objRef}>content</span>
</div>
);
}
componentDidMount() {
this.objRef.current.textContent = 'obj ref got'
}
}