Can't use React.findDOMNode function

前端 未结 4 1319
既然无缘
既然无缘 2021-01-11 10:06

For some reason, I\'m not able to use React.findDOMNode function. Browser complains about type error, saying React.findDOMNode is not a function. This is the code where this

相关标签:
4条回答
  • 2021-01-11 10:45

    The findDOMNode method has moved from react module to the react-dom module.

    So you have import or require the ReactDOM from react-dom module then replace

    var brand = React.findDOMNode(this.refs.brand).value.trim();
    

    With

    var ReactDOM = require('react-dom');
    var brand = ReactDOM.findDOMNode(this.refs.brand).value.trim();
    
    0 讨论(0)
  • 2021-01-11 10:55

    I would have commented on @romkyns answer but i don't have the rep.

    So, I have been following a tutorial:

    https://spring.io/blog/2015/09/15/react-js-and-spring-data-rest-part-2-hypermedia

    By changing the content from the newest version of the project on github. There was an error both in the code that relates to the answer I also voted up, but realized there was a mistake.

    So you must replace React.findDOMNode by ReactDOM.findDOMNode and not by deleting findDOMNode as you stated.

    I am not sure if this is due to a new version of Reactjs but this gave me the awaited result.

    0 讨论(0)
  • 2021-01-11 10:57

    React.findDOMNode(component) was introduced in React 0.13.0 as a replacement for component.getDOMNode().

    Make sure that you have React 0.13.1 installed. If you're using npm, you can run npm view react version to check which version of React is currently installed.

    0 讨论(0)
  • 2021-01-11 11:00

    In React 15 (and possibly some earlier versions, I'm not sure) you don't need findDOMNode to use refs at all:

    this.refs.brand.value
    

    is sufficient.

    0 讨论(0)
提交回复
热议问题