React - Get text of div on click without refs

前端 未结 4 1858
别跟我提以往
别跟我提以往 2020-12-30 06:05

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         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 06:30

    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 
    HEkudsbdsu
    } });

    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.

提交回复
热议问题