Reactjs Learning Series (use of React ref)

Source: Internet
Author: User

In the process of learning react, I always get confused by some nouns, but it is very simple to think about it, such as the react-defined refs, which is actually a way to get the DOM.

In react, a component is not a real DOM node, but rather a data structure that exists in memory, called the virtual Dom. Only when it is inserted into the document will it become the real DOM. According to the design of React, all DOM changes occur first on the virtual DOM, and then the actual changes are reflected in the real DOM, which is called Dom diff, which can greatly improve the performance of the Web page.

 var  mycomponent = React.createclass ({ Handleclick:  function   () { this  .refs.mytextinput.focus (); }, Render:  function   () { return   ( <div> <input type= "text" ref= "my TextInput "/> <input type=" button "value=" Focus the text input "Onclick={this.handleclick}/> </div> <mycomponent/>, document.getElementById (' Example '  

In the code above, the child nodes of the component mycomponent have a text input box to get the user's input. At this point, the real DOM node must be obtained, and the virtual DOM cannot be entered by the user. In order to do this, the text input box must have a ref attribute and then this.refs. [RefName] will return this true DOM node.

With this ref we can also get input values of input. It's that simple.

But in the process of use we remember: due to this.refs. The [RefName] property gets the real DOM, so you must wait until the virtual DOM is inserted into the document before you can use this property, or you will get an error. In the above code, by specifying the callback function for the Click event for the component, it ensures that the this.refs is not read until the Click event occurs in the real DOM. [RefName] property.

Original link: http://blog.csdn.net/jiangbo_phd/article/details/51758148

Reactjs Learning Series (use of React ref)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.