我正在使用 ReactJS 和 Flux 构建一个 Web 应用程序,我正在尝试使用findDOMNode方法 获取当前 div 的节点,但出现下一个错误:
Uncaught TypeError: React.findDOMNode is not a function
所以,我尝试使用getDOMNode并得到了同样的错误:
Uncaught TypeError: React.getDOMNode is not a function
我正在使用 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 版本:0.14.0
编辑
正如答案中所指出的,从 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;
但我遇到了另一个问题:
Uncaught Error: Invariant Violation: findDOMNode was called on an unmounted component.