reactjs – React.js:findDOMNode和getDOMNode之间的区别

前端之家收集整理的这篇文章主要介绍了reactjs – React.js:findDOMNode和getDOMNode之间的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有人可以告诉我有什么区别
React.findDOMNode(this.refs.email).value

this.refs.email.getDOMNode().value

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

组件.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中被删除(但不要引用我,因为我找不到一个很好的参考)。

编辑:更新以反映反应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

另请注意,对于React DOM组件,不再需要调用findDOMNode或getDOMNode,因为0.14:

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回购的相关代码和承诺:

> https://github.com/facebook/react/blob/a6d03f36a4a9e7c1e6688bdba89656f2e20e7df8/src/modern/class/ReactComponent.js#L101-L104
> https://github.com/facebook/react/commit/b46a6ce4bb8d6087ed424764f41fe4b8e248b3b4
> https://github.com/facebook/react/commit/fb23276178b28fdcb75aa22be013a91755f7ad0a

猜你在找的React相关文章