javascript React.findDOMNode 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29336178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React.findDOMNode comes as undefined
提问by user544079
I have
我有
<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} />
<input type="button" value="Clear" onClick={this.Clear} />
</div>
);
}
});
React.render(<Login />, document.getElementById('wrapper'))
</script>
回答by Felix Kling
React.findDOMNode
was introduced in React v0.13, so make sure you are using at least v0.13.
React.findDOMNode
是在 React v0.13 中引入的,所以请确保您至少使用 v0.13。
回答by svnm
In React v0.13use React.findDOMNode()
.
在 React v0.13 中使用React.findDOMNode()
.
In earlier versions e.g. v0.12you can use component.getDOMNode()
在早期版本中,例如v0.12,您可以使用component.getDOMNode()
this.refs.myRef.getDOMNode();
To support ES6 based patterns going forward, the React team added React.findDOMNode(component)
to be used in place of component.getDOMNode().
为了支持基于 ES6 的模式,React 团队添加React.findDOMNode(component)
了用于代替 component.getDOMNode()。
回答by Victor H?ggqvist
Not sure when it happened, but it seems to live in ReactDOM now ReactDOM.findDOMNode
.
不确定它何时发生,但它现在似乎存在于 ReactDOM 中ReactDOM.findDOMNode
。
See https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode.
请参阅https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode。
回答by Coding Duchess
React.findDOMNode
React.findDOMNode
has been deprecated, use
已弃用,请使用
ReactDOM.findDOMNode
ReactDOM.findDOMNode
in React 15.1 and up
在 React 15.1 及更高版本中
回答by Prakash Tiwari
This might not be very relevant for the code mentioned with the question above, however a couple of other checks from react's docs are:
这可能与上面问题中提到的代码不太相关,但是来自 react 文档的其他一些检查是:
- "When render returns null or false, findDOMNode returns null."
- "findDOMNode cannot be used on functional components."
- “当渲染返回 null 或 false 时,findDOMNode 返回 null。”
- “findDOMNode 不能用于功能组件。”
Hope this helps someone in need.
希望这可以帮助有需要的人。