liveBook · Manning

React.createElement

As explained in React Quickly, Second Edition

React.createElement is a React API function that creates an immutable description of an element from its type, properties, and children.

Create and render a heading

const heading = React.createElement('h1', null, 'Hello world!'); const root = ReactDOM.createRoot(document.getElementById('root')); root.render(heading); section 1.4.2

React.createElement(type, properties, children) creates a React element, not a DOM node. The type can be an HTML tag name such as 'h1' or a custom component. Pass an object for properties, or null when none are needed. ReactDOM renders the resulting element into a root. section 1.4.2 section 2.9

For nested UI, pass another React element as a child. An element can have several children: React.createElement('h1', null, 'Hello ', React.createElement('em', null, 'world'), '!') makes only “world” italic. Children can also be supplied as an array. section 2.3.2

Listing 2.1 Emphatically greeting the world
import React from "react";
import ReactDOM from "react-dom/client";
const world = React.createElement("em", null, "world");
const title = React.createElement("h1", null, "Hello ", world, "!");
const domElement = document.getElementById("root");
ReactDOM.createRoot(domElement).render(title);

See the complete emphasized greeting, from nested createElement calls through rendering. section 2.3.2

JSX offers shorter syntax for the same style of element construction: const title = <h1>Hello</h1> replaces a repeated createElement call. JSX must be transpiled into JavaScript before it runs. section 3.1.2

How React Quickly, Second Edition covers it

How deep it goes
Explains argument roles and nested children with working calls, but does not examine element internals.
Examples
Builds a rendered heading and a greeting with an italicized word inside a heading.
What it assumes
Assumes basic HTML and JavaScript while introducing React element creation through a first heading.
What you can do after
Build it from scratch: create and nest React elements, then render them with ReactDOM.
How it connects
Connects React elements to ReactDOM, custom components, properties, child elements, and JSX.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.