简体   繁体   English

反应引用-使用引用访问DOM节点

[英]React refs - accessing DOM nodes using refs

I'm trying to access DOM nodes of a react component using refs. 我正在尝试使用引用访问react组件的DOM节点。 I've already seen a lot of example doing that. 我已经看到了很多这样做的例子。 I decided to make my first app using React, and then I was stuck resolving this issue. 我决定使用React创建我的第一个应用程序,然后我不得不解决这个问题。

onDivHover:function()
{
    this.refs.myslider.getDOMNode().value = 20;
},

render: function() {
    return (
        <div className="sliderBox" OnMouseOver={this.onDivHover()}>
            <input ref="myslider" type="range" min={this.props.minimum} max={this.props.maximum} />
        </div>
    );
}

See, in the code I tried to change the value of slider using refs. 请参阅,在代码中,我尝试使用refs更改slider的值。 If I comment that one line inside onDivHover , I'm able to load the app successfully. 如果我在onDivHover内注释了其中一行,则可以成功加载该应用程序。 The error which is coming is React is unable to resolve myslider inside onDivHover . 即将出现的错误是React无法解析myslider内部的onDivHover

This seems crazy, as I've seen many examples using that. 这似乎很疯狂,因为我已经看到很多使用它的示例。 I might be missing something very basic and important. 我可能会错过一些非常基本和重要的内容。 Please help in resolving this. 请帮助解决此问题。

React事件需要一个函数,因此您应该传递一个引用,而不是执行它:

onMouseOver={this.onDivHover}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM