React 中 refs的使用

refs有两种使用方法
一、是用回调函数

class Test extends Component {
  testref() {
	const el =this.codeValue;
 }
    
    render(){
        return (
            <div ref={(e) => { this.codeValue = e }}></div>
        )
    }
}

二、使用字符串

class Test extends Component {
  testref() {
	const el =this.refs.codeValue;
 }
    render(){
        return (
            <div ref=“codeValue”></div>
        )
    }
}

使用第二种方法的话,有时候eslint会报错,所以还是推荐使用第一种方法

猜你喜欢

转载自blog.csdn.net/kelly0721/article/details/84645913