liveBook · Manning

JSX

As explained in Secrets of the JavaScript Ninja, Third Edition

JSX is a syntax extension that lets developers write XML-like markup within JavaScript code.

JSX lets JavaScript and HTML-like markup live together. It treats markup as a value: `const link = <a href="https://manning.com">Manning</a>;` can compile to a runtime call that creates an object. The exact output depends on the compiler and its settings. section 4.4

In React, components can return JSX based on their state. React handles the DOM updates, using a virtual DOM and reconciliation to compare element trees and update the page. This replaces direct DOM operations such as creating elements, appending children, and registering event listeners. section 4.4 section 4.5

Listing 4.3 An event list component in React
import React, { useState } from "react";
 
export default function EventList() { # A
  const [events, setEvents] = useState(["Initial event"]); # B
 
  return (
    <ul id="event-list">
      {events.map((str) => ( # C
        <li>{str}</li>       # C
      ))}                    # C
      <li>
        <input                                                # D
          onKeyDown={(evt) => {                               # D
            setEvents(                                        # D
              events.concat(`Event "keydown" fired with key = # D
  "${evt.key}"`),                                             # D
            );                                                # D
          }}                                                  # D
        />                                                    # D
      </li>
    </ul>
  );
}

This event-list component maps state into list items and updates that state when a key is pressed. section 4.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains markup-to-object compilation and React's state-driven DOM updates, including virtual-DOM reconciliation.
Examples
Uses a link compiled to a runtime call and an event-list component that adds keydown events to state.
What it assumes
Assumes familiarity with JavaScript, HTML, and DOM interaction while situating JSX after direct HTML and DOM API approaches.
What you can do after
Build interactive React components that render state as DOM without directly managing DOM operations.
How it connects
Connects JSX with JavaScript, HTML, the DOM, React components, state, and virtual-DOM reconciliation.

Also explained in

Compare how 9 books explain JSX →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.