美文网首页
react refs使用方式

react refs使用方式

作者: 戴发夹的格格巫 | 来源:发表于2021-01-05 11:20 被阅读0次

1.字符串形式

赋值:ref="foo", 使用: this.refs.foo

因为 string 类型的 refs 存在 一些问题。它已过时并可能会在未来的版本被移除

2. 回调函数

赋值: ref={(c) => this.foo = c},使用:this.foo

3.React.createRef()

赋值:ref={this.textInput},使用:this.textInput.current

当 ref 被传递给 render 中的元素时,对该节点的引用可以在 ref 的 current 属性中被访问

相关文章

网友评论

      本文标题:react refs使用方式

      本文链接:https://www.haomeiwen.com/subject/lrpioktx.html