user4831663
user4831663

Reputation:

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?

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>
        )
    },

Upvotes: 2

Views: 1049

Answers (3)

Vidhya Vasudevan
Vidhya Vasudevan

Reputation: 56

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">

It worked for me.

Hope you already found a solution for this. Cheers!

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128796

newText here needs to be either a string ("newText") or should use dot notation instead. Using just newText means you're trying to read the value of a variable with that name (which will return undefined).

Change:

this.refs[newText]

To:

this.refs["newText"]

Or:

this.refs.newText

Upvotes: 2

Amit
Amit

Reputation: 3289

use this.refs.newText rather than this.refs[newText].

Upvotes: 0

Related Questions