React - Get div text on click without links

Is there an example where we can get the value of a React element without using refs? Is it possible?

var Test = React.createClass({ handleClick: function() { alert(this.text); }, render: function() { <div className="div1" onClick={this.handleClick}> HEkudsbdsu </div> } }) 
+6
source share
2 answers

I found a working answer:

 var Hello = React.createClass({ handleClick: function(event) { alert(event.currentTarget.textContent); }, render: function() { return <div className="div1" onClick={this.handleClick}> HEkudsbdsu </div> } }); 
+19
source

I suppose you can just get the DOM Node with ReactDOM.findDOMNode(this); and then get it innerText or whatever you need from it.

 var Hello = React.createClass({ handleClick: function() { var domNode = ReactDOM.findDOMNode(this); alert(domNode.innerText); }, render: function() { return <div className="div1" onClick={this.handleClick}> HEkudsbdsu </div> } }); 

This is a slightly easy way to do this, but it will work.

Note that pre 0.14 React, you just use React , not ReactDOM .

+3
source

All Articles