'How to add size as custom attribute in React?

https://reactjs.org/blog/2017/09/08/dom-attributes-in-react-16.html As per this documentation, we do not need to have a data- as a prefix for a custom HTML attribute in JSX to appear in the actual DOM element without warning.

But when I try and use<div size="hello">my div element</div> it appears as <div>my div element</div> in actual DOM.

When I try <div Size="hello">my div element</div> then it appears correctly but it gives a warning.

What is the right way of adding size as a custom property on the HTML attribute?

example codepen here



Solution 1:[1]

Since the "size" attribute is a valid attribute for other tags but does not exist on the <div> element React strips it. In the documentation you referenced, it does state that you can prefix attributes with data-. "Just like before, React lets you pass data- and aria- attributes freely".

For obvious reasons, this is not recommended and should probably only be done if non-react scripts utilize the attribute.

To answer the question, if you have to store data in attributes, the best option is to prefix it with data-.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1