Code Reference

JSX

React · Reference cheat sheet

JSX

React · Reference cheat sheet


📋 Overview

JSX is syntax sugar for React.createElement / the JSX transform. It looks like HTML in JavaScript but follows JavaScript rules for expressions and attributes.

🔧 Core concepts

  • Expressions\{value\} inside tags; any JS expression is allowed.
  • Attributes — camelCase (className, htmlFor, onClick); strings in quotes or \{expr\}.
  • One parent — return a single root, fragment <>...</>, or array.
  • Self-closing<img />, <Component /> when no children.
  • Booleansdisabled=\{isOff\}; omit or pass true for presence flags.

💡 Examples

const name = "Ada";
const el = (
  <>
    <h1 className="title">Hello, {name}</h1>
    <img src="/avatar.png" alt="" />
    {items.length > 0 ? (
      <ul>
        {items.map((item) => (
          <li key={item.id}>{item.label}</li>
        ))}
      </ul>
    ) : (
      <p>Empty</p>
    )}
  </>
);
type IconProps = { size?: number };

function Icon({ size = 16 }: IconProps) {
  return <svg width={size} height={size} aria-hidden />;
}

⚠️ Pitfalls

  • classclassName; forhtmlFor.
  • style takes an object: style=\{\{ marginTop: 8 \}\}, not a CSS string (unless using libraries).
  • Comments: \{/* comment */\}, not &lt;!-- -->.
  • if statements aren’t expressions — use ternaries or extract variables.

On this page