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会报错,所以还是推荐使用第一种方法
本文详细介绍了React中Refs的两种使用方式:回调函数和字符串。第一种方法使用回调函数将元素引用传递给组件实例,而第二种方法通过字符串直接在组件中获取元素引用。由于第二种方法可能引发ESLint警告,因此推荐使用第一种方法。
1379

被折叠的 条评论
为什么被折叠?



