react/jsx-curly-spacing

Enforce or disallow spaces inside the curly braces of JSX attributes and children

fixable

value={ name } and value={name} mean the same thing. A codebase that mixes them is harder to scan and gets whitespace-only diffs. By default the rule doesn't allow spaces inside the braces of a prop or a spread. It doesn't check children.

--fix adds or removes the spaces.

Configure#

Off by default. To turn it on:

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

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"react/jsx-curly-spacing" = [
  "error",
  "never", # "always" or "never"
  {
    allowMultiline = true,
    # spacing = <object>,
  },
]

Examples#

example.jsx4 problems
const field = <Input value={ name } { ...rest } />;
example.jsxNo problems
const field = <Input value={name} {...rest} />;

const label = <Label>{ name }</Label>;

Options#

The option is "never" (default), "always", or an object:

OptionDefaultWhat it does
when"never""never" doesn't allow spaces inside the braces. "always" requires one on each side.
allowMultilinetrueAllow a line break right after { and right before }.
spacing.objectLiteralssame as when"always" or "never" for braces that hold an object literal, as in style={{ color }}.
attributestrueCheck props and spreads. false turns that off. An object with the three options above applies to props only.
childrenfalseCheck children. true turns that on. An object with the three options above applies to children only.

After "always" or "never", a second object can hold allowMultiline and spacing.

bunfig.toml
[lint.rules]
"react/jsx-curly-spacing" = ["error", { when = "always", children = true }]

With "always":

example.jsxwith "always"2 problems
const field = <Input value={name} />;
example.jsxwith "always"No problems
const field = <Input value={ name } />;

With children set to true, children follow the same setting as props:

example.jsxwith { "children": true }2 problems
const label = <Label>{ name }</Label>;

With allowMultiline set to false:

example.jsxwith { "allowMultiline": false }2 problems
const field = <Input value={
  name
} />;

spacing.objectLiterals lets {{ and }} stay together while other braces get spaces:

example.jsxwith "always", { "spacing": { "objectLiterals": "never" } }No problems
const box = <Box style={{ color: "red" }} title={ name } />;

When not to use it#

A formatter such as bun fmt or Prettier already fixes this spacing.