React.findDOMNode comes as undefined

React.findDOMNode comes as undefined

本文关键字:undefined as comes findDOMNode React      更新时间:2023-09-26

我有

<div id="wrapper"></div>
<script type="text/jsx">
/* @jsx React.DOM*/
var Login = React.createClass({
    Validate: function(){
        debugger;
        var username = React.findDOMNode(this.refs.username).trim();
        var password = React.findDOMNode(this.refs.password).trim();
        console.log('Username: ' + username + ''nPassword: ' + password);
        if(username == 'username' && password == 'password'){
            alert('Success');
        }
        else{
           alert('Failure');
        }
    },
    Clear: function(){
    },
    render: function(){
        return(
            <div className="container">
                Login 
                <p></p>
                Username: <input type="text" ref="username" /><br />
                Password: <input type="password" ref="password" /><br /><br />
                <input type="button" value="Submit" onClick={this.Validate} />&nbsp;&nbsp;
                <input type="button" value="Clear" onClick={this.Clear} />
            </div>
        );
    }
});
React.render(<Login />, document.getElementById('wrapper'))
</script>

React.findDOMNode是在React v0.13中引入的,因此请确保至少使用v0.13。

在Reactv0.13中使用React.findDOMNode()

在早期版本中,例如v0.12,您可以使用component.getDOMNode()

this.refs.myRef.getDOMNode();

为了支持未来基于ES6的模式,React团队添加了React.findDOMNode(component)来代替component.getDOMNode().

不确定它是什么时候发生的,但它现在似乎生活在ReactDOM ReactDOM.findDOMNode中。

请参阅https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode.

React.findDOMNode

已弃用,请使用

ReactDOM.findDOMNode

在React 15.1和上

这可能与上述问题中提到的代码不太相关,但来自react文档的其他几项检查是:

  1. "当render返回null或false时,findDOMNode返回null。"
  2. "findDOMNode不能用于功能组件。"

希望这能帮助到需要帮助的人。