加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:33:43 所属栏目:百科 来源:网络整理
导读:有人可以告诉我有什么区别 React.findDOMNode(this.refs.email).value 和 this.refs.email.getDOMNode().value 他们正在做同样的事情 – 获取元素的价值,但我应该在哪里使用哪一个。 组件.getDOMNode()已不再适用于React 0.13: Added new top-level API Re
有人可以告诉我有什么区别
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读