Sub topic 4
Attributes in JSX: className, style, and camelCase
This chapter assumes you know basic HTML attributes, JavaScript objects, and how to embed expressions in JSX with curly braces. JSX attributes look like HTML attributes, but they are really properties on a props object, so some names and value types differ.
Attribute names use camelCase
JSX is compiled to JavaScript, and attribute names become object keys. Because of that, React uses the DOM property names, written in camelCase. Two HTML names clash with JavaScript reserved words or DOM property names, so they change:
| HTML | JSX |
|---|---|
| `class` | `className` |
| `for` | `htmlFor` |
| `tabindex` | `tabIndex` |
| `onclick` | `onClick` |
| `maxlength` | `maxLength` |
Attributes that start with data- or aria- keep their hyphenated form, for example aria-label and data-id.
function SearchField() {
return (
<div className="field">
<label htmlFor="q">Search</label>
<input id="q" type="search" maxLength={50} aria-label="Search" />
</div>
);
}Strings and expressions
An attribute value is either a quoted string or an expression in curly braces. Use quotes for fixed text and braces for anything computed. Do not put quotes around braces, because src="{url}" passes the literal text {url}.
function Profile() {
const user = { name: 'Ada', imageUrl: 'https://example.com/ada.png' };
return (
<img
className="avatar"
src={user.imageUrl}
alt={user.name}
style={{ width: 64, height: 64 }}
/>
);
}Non-string values, such as numbers and booleans, also need braces: maxLength={50}. A boolean attribute written with no value, like <input disabled />, means disabled={true}.
The style attribute takes an object
In HTML, style is a string. In JSX, style takes a JavaScript object whose keys are camelCase CSS properties. The double braces are not special syntax. The outer pair marks a JSX expression, and the inner pair is the object literal.
Property names change from background-color to backgroundColor. A plain number is treated as pixels for most properties, so width: 64 becomes 64px. Use a string when you need another unit, such as width: '50%'.
function Banner({ highlighted }) {
const style = {
backgroundColor: highlighted ? 'gold' : 'white',
padding: 16,
width: '50%',
};
return <div style={style}>Sale ends today</div>;
}Building a className dynamically
className is a string, so you can build it with a template literal. Take care that a missing condition does not print the word undefined.
function Button({ primary, children }) {
const className = primary ? 'btn btn-primary' : 'btn';
return <button className={className}>{children}</button>;
}Common errors
- Writing
classinstead ofclassName. React warns in the console, and the styling is unreliable. - Passing a string to
style, such asstyle="color: red". React throws an error because it expects an object. - Using a hyphenated CSS name, such as
{ 'font-size': 12 }. UsefontSize. - Quoting braces, as in
alt="{user.name}", which renders the literal text.
Exercise
This component has three attribute mistakes. Rewrite the opening tag correctly: <label class="tag" for="email" style="font-size: 14px">.
Show answer
<label className="tag" htmlFor="email" style={{ fontSize: 14 }}>. class becomes className, for becomes htmlFor, and style must be an object with a camelCase key. The number 14 is treated as pixels.