简体   繁体   English

React.js-REF未定义

[英]React.js - REF undefined

When I test the following code I receive a console error that newText is undefined, am I declaring the val var correctly or am I missing something? 当我测试以下代码时,我收到一个控制台错误,即newText未定义,我是正确地声明了val var还是缺少了什么?

val = ReactDOM.findDOMNode(this.refs[newText]);

renderForm: function() {
        return (
            <div className="note">
                <textarea ref="newText" defaultValue={this.props.children} className="form-control"></textarea>
                <button onClick={this.save} className="btn btn-success btn-sm glyphicon glyphicon-floppy-disk"></button>
            </div>
        )
    },

newText here needs to be either a string ( "newText" ) or should use dot notation instead. 这里的newText必须是字符串( "newText" ),或者应该使用点表示法。 Using just newText means you're trying to read the value of a variable with that name (which will return undefined ). 仅使用newText意味着您尝试读取具有该名称的变量的值(该变量将返回undefined )。

Change: 更改:

this.refs[newText]

To: 至:

this.refs["newText"]

Or: 要么:

this.refs.newText

I know it's very late to answer this but I came across the same problem. 我知道回答这个问题已经很晚了,但是我遇到了同样的问题。

I added and update the following sources: src= " https://unpkg.com/react@15/dist/react.js "> src="https://unpkg.com/react-dom@15/dist/react-dom.js"> 我添加并更新了以下源:src =“ https://unpkg.com/react@15/dist/react.js ”> src =“ https://unpkg.com/react-dom@15/dist/react -dom.js“>

It worked for me. 它为我工作。

Hope you already found a solution for this. 希望您已经找到了解决方案。 Cheers! 干杯!

使用this.refs.newText而不是this.refs[newText]

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

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