react/jsx-curly-spacing
Enforce or disallow spaces inside the curly braces of JSX attributes and children
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:
[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.
[lint.rules]
"react/jsx-curly-spacing" = [
"error",
"never", # "always" or "never"
{
allowMultiline = true,
# spacing = <object>,
},
]Examples#
const field = <Input value={There should be no space after { name }There should be no space before } {There should be no space after { ...rest }There should be no space before } />;const field = <Input value={name} {...rest} />;
const label = <Label>{ name }</Label>;Options#
The option is "never" (default), "always", or an object:
| Option | Default | What it does |
|---|---|---|
when | "never" | "never" doesn't allow spaces inside the braces. "always" requires one on each side. |
allowMultiline | true | Allow a line break right after { and right before }. |
spacing.objectLiterals | same as when | "always" or "never" for braces that hold an object literal, as in style={{ color }}. |
attributes | true | Check props and spreads. false turns that off. An object with the three options above applies to props only. |
children | false | Check 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.
[lint.rules]
"react/jsx-curly-spacing" = ["error", { when = "always", children = true }]With "always":
"always"2 problemsconst field = <Input value={A space is required after {name}A space is required before } />;"always"No problemsconst field = <Input value={ name } />;With children set to true, children follow the same setting as props:
{ "children": true }2 problemsconst label = <Label>{There should be no space after { name }There should be no space before }</Label>;With allowMultiline set to false:
{ "allowMultiline": false }2 problemsconst field = <Input value={There should be no newline after {
name
}There should be no newline before } />;spacing.objectLiterals lets {{ and }} stay together while other braces get spaces:
"always", { "spacing": { "objectLiterals": "never" } }No problemsconst box = <Box style={{ color: "red" }} title={ name } />;When not to use it#
A formatter such as bun fmt or Prettier already fixes this spacing.