[英]React Exposing DOM Refs to Parent Components
We can use ref in 2 ways, first way is pass a string value as a ref and access the component as: 我们可以用2种方式使用ref,第一种方法是将字符串值作为ref传递并访问组件:
inputRef="input"
...
myInput = this.refs.input
But this is an old way and it may be deprecated in next versions. 但这是一种古老的方式,可能会在下一版本中弃用。 The second way is to pass a function to ref, which fired when a component is mounted and ready to work.
第二种方法是将函数传递给ref,该函数在安装组件并准备工作时触发。 Also, this function will receive the target element as a parameter.
此外,此函数将接收目标元素作为参数。
To clarify this moment let's write the same code in ES5: 为了澄清这一刻,让我们在ES5中编写相同的代码:
inputRef={ function(input) {_this.inputElement = input;}
So, in this case, you've added inputElement key to parent component that contains the input. 因此,在这种情况下,您已将inputElement键添加到包含输入的父组件。 And you can access it via
this.inputElement
您可以通过
this.inputElement
访问它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.