Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the different ReactDOM.findDOMNode(this.refs.a) vs this.refs.a?

Tags:

reactjs

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);
like image 684
Vimalesan Avatar asked Apr 16 '16 07:04

Vimalesan


People also ask

What is ReactDOM findDOMNode ()?

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 .

What is ReactDOM and what is the difference between ReactDOM and React?

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 does ReactDOM mean?

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()

What does ReactDOM render mean?

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.


1 Answers

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.

like image 114
gyzerok Avatar answered Oct 05 '22 13:10

gyzerok