一. ref 用于HTML标签元素中
<h2 ref=字符串/对象/函数>h2元素<h2>
其中,字符串形式 官方已不再推荐使用
1.对象形式
import React, { Component,createRef } from 'react'
export default class App extends Component {
constructor(){
super();
this.titleRef=createRef();
}
render() {
return (
<div>
<h2 ref={this.titleRef}>App组件内容</h2>
<button onClick={e=>this.btnApp()}>App按钮</button>
</div>
)
}
btnApp(){
this.titleRef.current.innerHTML="改变后的文本"
}
}
2.函数形式
import React, { Component} from 'react'
export default class App extends Component {
constructor(){
super();
this.titleEL=null;
}
render() {
return (
<div>