liveBook · Manning

JSX

As explained in Building User-Friendly DSLs

A JavaScript syntax extension that permits XML-like markup to be embedded in JavaScript expressions.

JSX is a JavaScript syntax extension for writing XML-like markup inside JavaScript expressions. It resembles HTML, but React requires attribute names to be valid JavaScript identifiers. JSX produces the React DOM that is rendered as an HTML DOM. section 4.0

A JSX element can call a React component as though it were an HTML element. Component attributes pass values as an object, which the component can destructure. section 4.1

Use JSX to build a projection from AST data:

  • Return markup directly from a component, such as a span containing placeholder text. section 4.1
  • Embed JavaScript expressions in braces to insert values extracted from an AST object. section 4.2.1
  • Use className rather than an HTML-style attribute name when applying CSS classes. section 4.0 section 4.2.1
  • Render repeated AST children with map and pass each child to a component, supplying a key for the rendered item. section 4.2.1

Projecting a record type

A Projection component can render a record name and recursively render its attributes: <span className="value">{astObject.settings["name"]}</span> and {astObject.settings["attributes"].map((attribute, index) => <Projection astObject={attribute} key={index} />)}. A switch on astObject.concept selects the JSX structure for each AST concept. section 4.2.1

Listing 4.2 src/frontend/index.jsx—using Projection
import React from "react" 
import { createRoot } from "react-dom/client" 

require("./styling.css") 

import rental from "../ch03/rental-AST"  
import { Projection } from "./projection"      #1

createRoot(document.getElementById("root")) 
    .render( 
         <Projection />     #2
    )

Shows a JSX component import and its use as the element rendered by React. section 4.1

How Building User-Friendly DSLs covers it

How deep it goes
Explains syntax, attributes, embedded expressions, mapping, recursion, and concept dispatch, but defers broader React and DOM details.
Examples
Uses Projection components, Rental record types, AST attributes, embedded expressions, CSS classes, and recursive array mapping.
What it assumes
Assumes a configured JavaScript frontend environment and framework familiarity, while explaining JSX usage through the projection implementation.
What you can do after
Builds a React JSX projection that renders AST data and passes nested values through component attributes.
How it connects
Connects JSX with JavaScript, React components, the React DOM, HTML DOM, ASTs, CSS, and templating.

Also explained in

Compare how 6 books explain JSX →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.