react/jsx-boolean-value

Enforce one way to write boolean attributes in JSX: disabled or disabled={true}

fixable

In JSX, <input disabled /> and <input disabled={true} /> mean the same thing. This rule makes your codebase stick to one of them. By default it wants the short form.

--fix removes or adds the ={true}.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"react/jsx-boolean-value" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"react/jsx-boolean-value" = [
  "error",
  "never", # "always" or "never"
  {
    always = [], # list of strings
    assumeUndefinedIsFalse = false,
  },
]

Examples#

example.jsx1 problem
const field = <input type="checkbox" checked={true} />;
example.jsxNo problems
const field = <input type="checkbox" checked />;

const other = <input type="checkbox" checked={false} disabled={isLocked} />;

Options#

bunfig.toml
[lint.rules]
"react/jsx-boolean-value" = ["error", "always"]
OptionWhat it does
"never" (default)Report checked={true}.
"always"Report checked without a value.
example.jsxwith "always"1 problem
const field = <input type="checkbox" checked />;
example.jsxwith "always"No problems
const field = <input type="checkbox" checked={true} />;

The second option is an object:

OptionDefaultWhat it does
always[]With "never": attributes that must have ={true}.
never[]With "always": attributes that must not have ={true}.
assumeUndefinedIsFalsefalseAlso report ={false} on an attribute that must not have a value. --fix removes the whole attribute.
example.jsxwith "never", { "always": ["selected"] }2 problems
const tab = <Tab selected closable={true} />;
example.jsxwith "never", { "always": ["selected"] }No problems
const tab = <Tab selected={true} closable />;
example.jsxwith "never", { "assumeUndefinedIsFalse": true }1 problem
const tab = <Tab closable={false} />;

Only turn on assumeUndefinedIsFalse if your components treat a missing prop like false. A prop that defaults to true changes meaning when the attribute is removed.