liveBook · Manning

Attribute syntax

As explained in React Quickly, Second Edition

A markup-like syntax that assigns values to attributes or properties inside an element tag.

Attribute syntax assigns values inside JSX element tags with key=value pairs. It works for standard HTML attributes and properties passed to custom React components. section 3.2.5

Use the same tag-based pattern for fixed and dynamic values:

  • Write hardcoded values such as href="//react.dev" directly in the tag. section 3.2.5
  • Wrap dynamic JavaScript values in curly braces, such as href={this.props.url} or title={this.props.label}. section 3.2.5
  • Pass values to custom components with the same syntax, then read them through component properties. section 3.2.5

A dynamic profile link

A ProfileLink component can turn parent-supplied url and label values into anchor properties: <a href={this.props.url} title={this.props.label}>Profile</a>. Create it with <ProfileLink url="/users/johnny" label="Profile for Johnny" />. section 3.2.5

class ProfileLink extends React.Component {
 render() {
   return (
     <a
       href={this.props.url}
       title={this.props.label}
       target="_blank">Profile
     </a>
   );
 }
}

Inspect a complete component that uses dynamic attribute values and a fixed target property. section 3.2.5

How React Quickly, Second Edition covers it

How deep it goes
Explains fixed values, brace-delimited dynamic values, parent-supplied properties, and custom components.
Examples
Uses anchors, Link, ProfileLink, href, title, url, label, and parent-created component instances.
What it assumes
Builds from familiar HTML/XML attribute syntax and earlier exposure to JSX.
What you can do after
Apply it via JSX to render standard elements and pass changing values into custom components.
How it connects
Connects JSX with HTML/XML attributes, React components, component properties, and child nodes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.