liveBook · Manning

React

As explained in Secrets of the JavaScript Ninja, Third Edition

React is an open-source JavaScript library for building user interfaces from declarative components.

React is a JavaScript library for building web interfaces with JSX, which combines JavaScript and HTML-like syntax. Instead of manually creating elements, appending nodes, and attaching event listeners, describe interface output in terms of state and let React update the DOM. section 1.2.1 section 4.4

JSX compiles to JavaScript objects; the exact conversion depends on the compiler and its settings. React uses component elements to build a virtual DOM, then reconciles it with the previous version and updates the page DOM to match. section 4.4 section 1.2.1

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
Traces JSX from syntax to compiled objects and virtual-DOM reconciliation without specifying the exact compiler output.
Examples
Demonstrates a JSX anchor, a stateful event-list component, and a counter with an increment button.
What you can do after
Build interactive interfaces by expressing component output in JSX and updating state to have React synchronize the DOM.
How it connects
Connects JSX, the DOM, and reconciliation with Angular, Vue, and Svelte as alternative UI frameworks.

Also explained in

Compare how 7 books explain React →

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.