Javascript ReactJS findDOMNode 和 getDOMNode 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33031516/
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
ReactJS findDOMNode and getDOMNode are not functions
提问by Victor Castillo Torres
I'm building a web-app with ReactJS and Flux and I'm trying to get the node of my current div using the method findDOMNodeand I get the next error:
我正在使用 ReactJS 和 Flux 构建一个 web 应用程序,我正在尝试使用findDOMNode方法获取当前 div 的节点, 但出现下一个错误:
Uncaught TypeError: React.findDOMNode is not a function
So, I tried to use getDOMNodeand I get the very same error:
所以,我尝试使用getDOMNode并且我得到了同样的错误:
Uncaught TypeError: React.getDOMNode is not a function
I'm using npm to build the JS, the code where I use these methods:
我正在使用 npm 来构建 JS,我使用这些方法的代码:
var React = require('react');
var stores = require('../stores');
var MessagesUserContainer = require('./messageusercontainer');
var ChatStore = stores.ChatStore;
var owner = stores.getOwner();
var MessagesList = React.createClass({
getInitialState: function(){
return {'muc': []};
},
componentDidUpdate: function(){
var node = React.findDOMNode(this); //Error here
node.scrollTop = node.scrollHeight;
},
render: function(){
return (
<div className="chatScroll">
{this.state.muc}
</div>
)
}
});
module.exports = MessagesList;
ReactJS verion: 0.14.0
ReactJS 版本:0.14.0
EDIT
编辑
As pointed out in the answers, the DOM library as of v0.14.0 is out of the React core, so I made a few changes to my code:
正如答案中所指出的,从 v0.14.0 开始的 DOM 库不在 React 核心之外,所以我对我的代码进行了一些更改:
var React = require('react');
var ReactDOM = require('react-dom');
var stores = require('../stores');
var MessagesUserContainer = require('./messageusercontainer');
var ChatStore = stores.ChatStore;
var owner = stores.getOwner();
var MessagesList = React.createClass({
getInitialState: function(){
return {'muc': []};
},
componentDidUpdate: function(){
var node = ReactDOM.findDOMNode(this);
node.scrollTop = node.scrollHeight;
},
render: function(){
return (
<div className="chatScroll">
{this.state.muc}
</div>
)
}
});
module.exports = MessagesList;
But I got another problem:
但我遇到了另一个问题:
Uncaught Error: Invariant Violation: findDOMNode was called on an unmounted component.
回答by user120242
Changed in latest React:
在最新的 React 中更改:
ReactDOM.findDOMNode
https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode
https://facebook.github.io/react/docs/top-level-api.html#reactdom.finddomnode
It is in the react-dom
package. Note that ReactDOMServer has also been moved into another package. Probably in preparation for React related platform-specific APIs (such as React native).
它在react-dom
包裹里。请注意, ReactDOMServer 也已移动到另一个包中。可能是为了准备 React 相关平台特定的 API(例如 React Native)。
To import/ require the package:
要导入/需要包:
import ReactDOM from "react-dom";
or
或者
var ReactDOM = require('react-dom').
回答by Pawel
In react v0.14
DOM library has been moved from React.js itself. There are some BC changes, but if you wrote your code in a proper way then changes won't be painful. Please read here for full overview: https://facebook.github.io/react/blog/#major-changes
在v0.14
React 中,DOM 库已从 React.js 本身移出。有一些 BC 更改,但是如果您以正确的方式编写代码,那么更改将不会很痛苦。请在此处阅读完整概述:https: //facebook.github.io/react/blog/#major-changes