Skip to content
Tutorials Center
My learning

Topics

React Reference ยท JSX Syntax and Rules

JSX attributes

Reference for JSX attributes: quoted strings, expressions in braces, the boolean shorthand, camelCase names, data and aria attributes, and values React leaves out.

Attributes in JSX become props. On a DOM tag, React then turns the props into DOM attributes and properties; on a component, the props are simply handed to your function. The syntax has three value forms: a quoted string (type="email"), an expression in braces (maxLength={limit}) and no value at all (disabled), which means true.

Names follow JavaScript, not HTML. Most are camelCase (tabIndex, readOnly, onClick), and two differ from their HTML spelling because the originals are reserved words: className for class and htmlFor for for. Attributes that contain a hyphen, data-* and aria-*, keep their dashes.

JSX
<input type="email" maxLength={limit} required className="field" data-testid="email" />

Try it

The three value forms
Which values become attributes

Syntax

JSX
<tag name="literal string" />
<tag name={expression} />
<tag booleanName />            // same as booleanName={true}
<tag data-name="x" aria-name="y" />
Form Meaning
name="text" A string literal. It is not an expression, and backslash escapes are not processed; HTML entities such as &amp; are decoded.
name={value} Any JavaScript value. Numbers, booleans, objects and functions keep their type.
name Shorthand for name={true}.
name={false}, , For known boolean attributes (hidden, disabled, checked): the attribute is left out. For aria-* and data-*, false is written as the string "false"; null and undefined are left out.

Errors and edge cases: passing true to a non-boolean attribute (for example title={true}) logs a warning in development and omits it. Functions and symbols as attribute values on DOM elements are also dropped, except for event handlers (onClick and friends).

Usage notes

  • Rename the exceptions. className, htmlFor, and defaultValue/defaultChecked for uncontrolled fields. See className and htmlFor.
  • style takes an object with camelCase keys: style={{ backgroundColor: 'tan' }}. Numbers get px for properties that expect lengths. A CSS string throws an error.
  • Quotes. Use double quotes for string attributes; pick the other quote type if the text contains one. Inside braces you can use any JavaScript string.
  • Strings versus numbers. tabIndex="0" and tabIndex={0} both work on DOM elements, but for your own components the type is what you wrote, so size="3" is the string "3".
  • Custom components receive everything, including attributes that look like HTML. <Button data-id="3" /> gives the component props['data-id'].
  • Unknown attributes on DOM elements are passed through in React 16 and later if they are strings or numbers, so <div foo="bar"> renders foo="bar".
  • Event props (onClick) are camelCase function values, not strings. onclick="..." is not supported.
  • No duplicate props. If an attribute appears twice, the later one wins. Spread attributes follow the same left to right rule, see JSX spread attributes.
  • Attribute order is irrelevant for the DOM, but readable code puts key and ref first, then the identity props, then handlers.