If getting element object directly from this.refs then Why ReactDOM.findDOMNode?
var HelloMessage = React.createClass({
click:function(){
console.log(ReactDOM.findDOMNode(this.refs.a))
console.log(ReactDOM.findDOMNode(this.refs.b))
console.log(this.refs.a)
console.log(this.refs.b)
},
render: function() {
return <div><div onClick={this.click}>click</div>Hello {this.props.name}{this.props.no?<div ref="a">{this.props.no}</div>:<div ref="b"></div>}</div>;
}
});
ReactDOM.render(<HelloMessage name="John" no={1}/>, mountNode);
findDOMNode() findDOMNode is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction. It has been deprecated in StrictMode .
React library is responsible for creating views and ReactDOM library is responsible to actually render UI in the browser. Include these two libraries before your main JavaScript file. While learning How React works, we'll build a small application using react and ReactDOM.
What is ReactDOM? ReactDOM is a package that provides DOM specific methods that can be used at the top level of a web app to enable an efficient way of managing DOM elements of the web page. ReactDOM provides the developers with an API containing the following methods and a few more. render()
The ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.
From the usage perspective there is no difference between these.
ReactDOM.findDOMNode
was a way of getting ref in the past. Currently it's deprecated and this.refs
is how you should do it now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With