Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's data-reactid attribute in html?

While I was going through the HTML of some pages, I noticed that some of them use this attribute "data-reactid" like :

 <a data-reactid="......" ></a> 

What is that attribute and what is its function ?

like image 531
Ayman El Temsahi Avatar asked Jul 11 '13 05:07

Ayman El Temsahi


People also ask

What is data Reactid in HTML?

The data-reactid attribute is a custom attribute that react can easily identify its components within the DOM. Just like the HTML “classes” and “id” attributes, “data-reactid” helps in uniquely identifying the element .

What is data value attribute in HTML?

The HTML data value attribute is used to Specify the machine-readable translation of the content of the element.

What is data text attribute?

Definition and Usage The data-* attribute is used to store custom data private to the page or application. The data-* attribute gives us the ability to embed custom data attributes on all HTML elements.

What is data attribute used for?

data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, or extra properties on DOM.


1 Answers

The data-reactid attribute is a custom attribute used so that React can uniquely identify its components within the DOM.

This is important because React applications can be rendered at the server as well as the client. Internally React builds up a representation of references to the DOM nodes that make up your application (simplified version is below).

{   id: '.1oqi7occu80',   node: DivRef,   children: [     {       id: '.1oqi7occu80.0',       node: SpanRef,       children: [         {           id: '.1oqi7occu80.0.0',           node: InputRef,           children: []         }       ]     }   ] } 

There's no way to share the actual object references between the server and the client and sending a serialized version of the entire component tree is potentially expensive. When the application is rendered at the server and React is loaded at the client, the only data it has are the data-reactid attributes.

<div data-reactid='.loqi70ccu80'>   <span data-reactid='.loqi70ccu80.0'>     <input data-reactid='.loqi70ccu80.0' />   </span> </div> 

It needs to be able to convert that back into the data structure above. The way it does that is with the unique data-reactid attributes. This is called inflating the component tree.

You might also notice that if React renders at the client-side, it uses the data-reactid attribute, even though it doesn't need to lose its references. In some browsers, it inserts your application into the DOM using .innerHTML then it inflates the component tree straight away, as a performance boost.

The other interesting difference is that client-side rendered React ids will have an incremental integer format (like .0.1.4.3), whereas server-rendered ones will be prefixed with a random string (such as .loqi70ccu80.1.4.3). This is because the application might be rendered across multiple servers and it's important that there are no collisions. At the client-side, there is only one rendering process, which means counters can be used to ensure unique ids.

React 15 uses document.createElement instead, so client rendered markup won't include these attributes anymore.

like image 191
Dan Prince Avatar answered Oct 26 '22 23:10

Dan Prince