javascript 不能使用 React.findDOMNode 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29443889/
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
Can't use React.findDOMNode function
提问by LazyDal
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 happens:
出于某种原因,我无法使用 React.findDOMNode 函数。浏览器抱怨类型错误,说 React.findDOMNode 不是一个函数。这是发生这种情况的代码:
var React = require('react');
var Backbone = require('backbone');
var Car = require('models/car');
var NewCarForm = React.createClass({
handleSubmit: function(e) {
e.preventDefault();
var brand = React.findDOMNode(this.refs.brand).value.trim();
...
this.props.handleNewCar(new Car({brand: brand, model:model, name:name, kmTraveled:odometer, litresSpent:litres}));
return;
},
render: function() {
console.log("Inside NewCarForm");
return (
<form className="contentSection" onSubmit={this.handleSubmit}>
<input type="text" placeholder="Car Brand" ref="brand" />
...
<input type="submit" value="Post" />
</form>
);
}
});
module.exports = NewCarForm;
This is the only module where I try to use this function. The rest of React works fine, so I have no idea what could be the problem here.
这是我尝试使用此功能的唯一模块。React 的其余部分工作正常,所以我不知道这里可能有什么问题。
回答by Roman Starkov
In React 15 (and possibly some earlier versions, I'm not sure) you don't need findDOMNode
to use refs at all:
在 React 15(可能还有一些早期版本,我不确定)中,您根本不需要findDOMNode
使用 refs:
this.refs.brand.value
is sufficient.
足够了。
回答by Alexandre Kirszenberg
React.findDOMNode(component)
was introduced in React 0.13.0
as a replacement for component.getDOMNode()
.
React.findDOMNode(component)
在 React 中被引入0.13.0
作为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.13.1
安装了React 。如果您使用的是 npm,您可以运行npm view react version
以检查当前安装的 React 版本。
回答by Nahid
The findDOMNode
method has moved from react
module to the react-dom
module.
该findDOMNode
方法已从一个react
模块移到另一个react-dom
模块。
So you have import or require the ReactDOM from react-dom
module then replace
所以你从react-dom
模块导入或需要 ReactDOM然后替换
var brand = React.findDOMNode(this.refs.brand).value.trim();
With
和
var ReactDOM = require('react-dom');
var brand = ReactDOM.findDOMNode(this.refs.brand).value.trim();
回答by Santa Clauze
I would have commented on @romkyns answer but i don't have the rep.
我会对@romkyns 的回答发表评论,但我没有代表。
So, I have been following a tutorial:
所以,我一直在关注一个教程:
https://spring.io/blog/2015/09/15/react-js-and-spring-data-rest-part-2-hypermedia
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.
通过更改 github 上最新版本项目的内容。与我也投票赞成的答案相关的代码中存在错误,但意识到存在错误。
So you must replace React.findDOMNode
by ReactDOM.findDOMNode
and not by deleting findDOMNode
as you stated.
所以,你必须更换React.findDOMNode
的ReactDOM.findDOMNode
,而不是删除findDOMNode
,你说。
I am not sure if this is due to a new version of Reactjs but this gave me the awaited result.
我不确定这是否是由于 Reactjs 的新版本,但这给了我期待的结果。