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}Value must be omitted for boolean attribute checked />;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"]| Option | What it does |
|---|---|
"never" (default) | Report checked={true}. |
"always" | Report checked without a value. |
example.jsxwith
"always"1 problemconst field = <input type="checkbox" checkedValue must be set for boolean attribute checked />;example.jsxwith
"always"No problemsconst field = <input type="checkbox" checked={true} />;The second option is an object:
| Option | Default | What it does |
|---|---|---|
always | [] | With "never": attributes that must have ={true}. |
never | [] | With "always": attributes that must not have ={true}. |
assumeUndefinedIsFalse | false | Also report ={false} on an attribute that must not have a value. --fix removes the whole attribute. |
example.jsxwith
"never", { "always": ["selected"] }2 problemsconst tab = <Tab selectedValue must be set for boolean attribute selected closable={true}Value must be omitted for boolean attribute closable />;example.jsxwith
"never", { "always": ["selected"] }No problemsconst tab = <Tab selected={true} closable />;example.jsxwith
"never", { "assumeUndefinedIsFalse": true }1 problemconst tab = <Tab closable={false}Value must be omitted for false attribute: closable />;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.