[Web Front end] React Advanced Tutorials (ES6)--(2) understanding of the latest changes in refs

Source: Internet
Author: User

cp:53384561

1. What is refs in Reactjs

In react, a component is not a real DOM node, but rather a data structure that exists in memory called the virtual DOM

DOM). Only when it is inserted into the document will it become the real DOM. According to React's design, all DOM changes are first

Occurs on the virtual DOM, and then the actual changes are reflected on the real DOM, which is called Dom diff, which

Can greatly improve the performance of Web pages.

If we want to be in the virtual Dom (at this point the DOM is not converted to true DOM), take an element, at this time cannot pass

JS's getelementbyxxx this form.

2.ReactJS Pre-procedure

Early in the virtual DOM phase takes elements, the syntax is as follows:

function Hello () {    Handleclick:function() {       this. Refs.myinput.fucus ();    }     return <input ref= "Myinput"/>}

From the above we can know that refs can be passed. [Refsname] to get the elements in the virtual DOM.

3.ReactJS the latest version, for the definition of refs

The definition of refs has not changed in the latest version, but the use of syntax has changed a lot.

Now, refs in the component is a callback function.

This function, in the component generation period (mounted)

is automatically executed, the parameters of this callback function are the elements themselves:

function Hello () {   Handleclick:function() {   }   return <input ref={(input) =>{this. myinput=input;}} />}

In this example, the function of ref passes in the INPUT element itself and assigns the this.myinput to the INPUT element itself.

The destruction period of the element (unmounted) is also performed, but at the time of destruction, only NULL is returned.

[Web Front end] React Advanced Tutorials (ES6)--(2) understanding of the latest changes in refs

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.