Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data attribute without value in react create element?

I am adding div to the DOM as below,

React.createElement("div", { className: "test"});

The rendered html should be as below

<div class="test" data-test>
</div>

Where can I add data-test in the createElement?

like image 277
Raviteja Avatar asked Apr 14 '17 11:04

Raviteja


2 Answers

If you refer to the doc on createElement API, you can insert the value to your data-test prop like this:

React.createElement('div', {className: 'test', 'data-test': 'some value'});
like image 69
Mμ. Avatar answered Oct 15 '22 20:10

Mμ.


You can't do that because attribute without any value means that this attribute has value true in JSX. Just don't pass data-test and this.props['data-test'] will be undefined in your component. Or if you need to have this attribute with empty value then just add it to your default values in component definition.

defaultProps: {
    'data-test': ''
}
like image 35
Eugene Tsakh Avatar answered Oct 15 '22 19:10

Eugene Tsakh