Skip to main content
JSX is a syntax extension for JavaScript that lets you write HTML-like markup inside JavaScript files. Preact supports JSX and provides a lightweight implementation.

What is JSX?

JSX allows you to write UI components using familiar HTML syntax:
This is more readable than the equivalent JavaScript:

How JSX works

JSX is not valid JavaScript. It must be transformed by a compiler (like Babel or TypeScript) into function calls.

The createElement function

Preact’s createElement() function (aliased as h()) converts JSX into VNodes (src/create-element.js:16):

JSX transformation

This JSX:
Gets transformed to:
The h function is Preact’s alias for createElement, inspired by hyperscript conventions.

Configuring JSX

You need to configure your build tool to transform JSX and specify Preact as the JSX factory.

Babel configuration

Add the React JSX transform plugin to your .babelrc or babel.config.js:
With this configuration, you need to import h in every file that uses JSX:

TypeScript configuration

Configure TypeScript in your tsconfig.json:
tsconfig.json
TypeScript requires you to import h even if you don’t use it directly, as it’s referenced in the transpiled output.

Automatic JSX runtime

Modern build tools support the automatic JSX runtime, which eliminates the need to import h:
.babelrc
With automatic runtime, you can write JSX without any imports:

JSX expressions

You can embed any JavaScript expression in JSX using curly braces:

Conditional rendering

JSX attributes

JSX attributes use camelCase naming, except for data-* and aria-* attributes:
Use className instead of class, as class is a reserved keyword in JavaScript.

Special attributes

Preact handles several attributes specially:
  • className - Sets the element’s CSS class
  • style - Accepts an object of CSS properties
  • dangerouslySetInnerHTML - Sets raw HTML (use with caution)
  • key - Helps Preact identify elements in lists
  • ref - Gets a reference to the DOM node

Children

Everything between an opening and closing tag becomes the children prop:
Children can be:
  • Strings and numbers
  • JSX elements
  • Arrays of elements
  • Functions (render props)
  • null, undefined, or false (renders nothing)

Fragments

Fragments let you group multiple elements without adding extra DOM nodes (src/create-element.js:77):

Using fragments

The <> short syntax requires configuring your build tool to recognize it as a Fragment.

Lists and keys

When rendering lists, each element should have a unique key prop:
Keys help Preact identify which items have changed:

Real-world example

Here’s a complete example from Preact’s demo (demo/index.jsx:68):
This example demonstrates:
  • Class component with JSX
  • Props destructuring in render
  • Nested JSX elements
  • Component composition
  • Conditional rendering via Router

JSX gotchas

Self-closing tags

Always close tags, even for elements that don’t have children:

JavaScript reserved words

Some HTML attributes conflict with JavaScript keywords:
  • Use className instead of class
  • Use htmlFor instead of for

Comments

Use JavaScript comments inside JSX expressions: