Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using boolean-value of attributes in JSX

I have a React.js project. I want to use data-picker plugin which require this style of input-attributes:

<input data-enable-time=true />

But webpack doesn't compile the app, when true is without quotes. Plugin doesn't work, when true with quotes. What I should do?

UPD.

Yes, I run picker in componentDidMount() It works, but displaying only date, without time.

import React, {Component} from 'react'
const Flatpickr = require('flatpickr');

export default class Date extends Component {

  componentDidMount() {
    let dateInput = document.getElementById('fPicker');
    //init picker
    new Flatpickr(dateInput);

  }

  render() {
    return(
      <div className="dateInputContainer">
        <input id="fPicker" className="flatpickr" data-enable-time="true" />
     </div>
    )
  }
}

But data-enable-time="true" doesn't work.

like image 872
Yarik Genza Avatar asked Jul 29 '16 13:07

Yarik Genza


People also ask

How do you pass a boolean value in React?

To pass a boolean as props to a component in React, wrap the boolean in curly braces, e.g. <Child bool={true} /> . All props you pass to a component that are not of type string have to be wrapped in curly braces.

What are boolean attributes?

A Boolean attribute is an attribute that can only be true or false. How does a Boolean attribute work? According to the HTML specification: The presence of a boolean attribute on an element represents the “true” value, and the absence of the attribute represents the “false” value.

What is a JSX attribute?

Attributes in JSX: JSX allows us to use attributes with the HTML elements just like we do with normal HTML. But instead of the normal naming convention of HTML, JSX uses camelcase convention for attributes. For example, class in HTML becomes className in JSX.


3 Answers

You can just omit the value assignment; the attribute will be assigned a value of true by default.

Instead of doing any of:

<input data-enable-time=true /> <input data-enable-time='true' /> <input data-enable-time={true} /> 

Do:

<input data-enable-time /> 

This approach avoids the warning Value must be omitted for boolean attributes [react/jsx-boolean-value] when the code is linted via eslint-plugin-react. (See: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-boolean-value.md)

like image 157
Jon Schneider Avatar answered Sep 24 '22 03:09

Jon Schneider


According to the HTML spec, there is no difference between data-enable-item=true and data-enable-item="true". They will function exactly the same in browsers. Because HTML attributes without quotes are practically never used and can lead to a number of issues, React always uses quoted attributes.

In the snippet below you can check that they do indeed have the exact same effect.

I suspect your plugin not working is probably because you are loading your plugin the wrong way, and not because of the data attribute style. Are you sure you're only starting the date picker after the component has been mounted (for example, in componentDidMount)?

var withoutQuotes = document.getElementById('input-no-attribute-quotes');
var withQuotes = document.getElementById('input-with-attribute-quotes');

console.log('Are the data attributes for test exactly the same? ' + (withoutQuotes.dataset.test === withQuotes.dataset.test ? 'Yes.' : 'No.'));
console.log('Data attributes without quotes\n', withoutQuotes.dataset);
console.log('Data attributes with quotes\n', withQuotes.dataset);
<input id=input-no-attribute-quotes data-test=true />
<input id="input-with-attribute-quotes" data-test="true" />
like image 36
Ambroos Avatar answered Sep 25 '22 03:09

Ambroos


Try something like this:

<input data-enable-time={true} />
like image 40
ksnietka Avatar answered Sep 22 '22 03:09

ksnietka