loan-front-team / data2graph

3 stars 1 forks source link

this.refs 和 ReactDOM.findDOMNode 区别是什么? #13

Open lulu27753 opened 6 years ago

lulu27753 commented 6 years ago

1、ref添加到component上获取的是component实例,添加到原生HTML上获取的是DOM 官方原文:

when attaching a ref to a DOM component like <div/>,you get the DOM node back; when attaching a ref to a composite component like <TextInput />, you will get the React class instance. In the latter case, you can call methods on that component if any are exposed in its class definition.

PS:新版本的React已经不推荐使用ref string,最好使用ref callback 2、ReactDOM.findDOMNode,当参数是DOM,返回值就是该DOM(这个没啥用);当参数是Component获取的是该Component render方法中的DOM 3、区别: refs从组件内部获取组件内部的DOM;ReactDOM.findDOMNode从组件外获取组件渲染的DOM。 refs可以拿子组件的props/state/方法;findDOMNOde拿到的是原生的标签相关属性。