Is there an example where we can retrieve the value of a React element without the usage of refs? Is that possible?
var Test = React.createClass({
handle
If you want to retrieve text, use innerText
property, if html – innerHTML
.
Example:
handleClick: function(e) {
alert(e.currentTarget.innerHTML);
}
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>
}
});
Try this way.
handleMenuItemClick = (event) => {
console.log(event.target.textContent);
};
<option onClick={event => this.handleMenuItemClick(event)}>
item
</option>
I suppose you can just get the DOM Node with ReactDOM.findDOMNode(this);
, and then get its 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 bit of a runabout way of doing it, but it will work.
Note that pre 0.14 React, you'll just be using React
and not ReactDOM
.