React.js: findDOMNode 和 getDOMNode 的区别

React.js: The difference between findDOMNode and getDOMNode

谁能告诉我

和两者有什么区别
React.findDOMNode(this.refs.email).value

this.refs.email.getDOMNode().value

他们在做同样的事情 - 获取元素的值,但我应该在哪里使用哪个。

第二个是用于访问 refs DOM 节点的旧 API,第一个是新方法。所以如果你使用的是最新版本的 React,你应该使用第一个。

component.getDOMNode() 从 React 0.13 开始被弃用:

Added new top-level API React.findDOMNode(component), which should be used in place of component.getDOMNode(). The base class for ES6-based components will not have getDOMNode. This change will enable some more patterns moving forward.

通过http://facebook.github.io/react/blog/2015/03/10/react-v0.13.html#new-features

它可能会在 React 的未来版本中被删除(但不要引用我的话,因为我找不到好的参考)。


编辑:更新以反映 React 0.14

getDOMNode() 在 0.13 和 0.14 中抛出警告,在 0.15 中将被完全移除:

With each returned DOM node, we've added a getDOMNode method for backwards compatibility that will work with a warning until 0.15.

通过https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#new-deprecations-introduced-with-a-warning

另请注意,自 0.14 起,React DOM 组件不再需要调用 findDOMNodegetDOMNode

The other big change we’re making in this release is exposing refs to DOM components as the DOM node itself. That means: we looked at what you can do with a ref to a React DOM component and realized that the only useful thing you can do with it is call this.refs.giraffe.getDOMNode() to get the underlying DOM node. Starting with this release, this.refs.giraffe is the actual DOM node. Note that refs to custom (user-defined) components work exactly as before; only the built-in DOM components are affected by this change.

通过https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#dom-node-refs


GitHub 上 React 仓库的相关代码和提交: