Skip to curriculum
Jump to the editor

17.6. Render DOM from a tree description

Medium
Topics
25–40 min

UI libraries describe the page as plain objects (what JSX compiles to) and turn those objects into real DOM nodes. Writing that step yourself shows what every prop really becomes: an attribute, a property, a listener, or a style, and why SVG needs special care.

Write render(description) in render.js. It returns new DOM nodes; it does not put them in the page.

Signature

JAVASCRIPT
function render(description) // returns a Node

description = {
  type: "button",                              // a tag name, or a function (a component)
  props: { className: "btn", onClick: save },  // optional
  children: ["Save"],                          // optional
};

A child is a description, a string, a number, an array of children, or null, undefined, true or false.

Requirements

  1. A string or a number renders as a text node (0 renders as "0").
  2. A description with a string type renders as that element; missing props and children mean none.
  3. Props become attributes with string values, with these exceptions:
    • className sets class, and htmlFor sets for.
    • true sets an empty attribute; false, null and undefined set nothing.
    • style as an object sets inline styles (camelCase names, and custom properties that start with --); as a string it is the style attribute.
    • A prop that starts with on and holds a function adds an event listener: onClick listens for click, onKeyDown for keydown. An on prop that is not a function is ignored.
    • value and checked are set as properties, so a field shows them.
    • key is not rendered.
  4. Children are rendered in order and appended. null, undefined, true and false render nothing; arrays of children are flattened at any depth.
  5. Text is always text: markup in a string never becomes elements.
  6. An array renders as a DocumentFragment; null, undefined or a boolean renders as an empty DocumentFragment.
  7. svg and every element inside it are created in the SVG namespace (http://www.w3.org/2000/svg), with attribute names kept as written (viewBox). Elements outside it are HTML.
  8. When type is a function, it is called with the props plus children, and what it returns is rendered (it may return null).
  9. Any other type (missing, empty, a number, an object) throws a TypeError.
  10. Each call creates new nodes and does not change the description.

Examples

JAVASCRIPT
render({
  type: "label",
  props: { className: "field", htmlFor: "email" },
  children: ["Email ", { type: "input", props: { id: "email", required: true, value: "a@b.c" } }],
});
// <label class="field" for="email">Email <input id="email" required=""></label>
// and the input's value is "a@b.c"
JAVASCRIPT
render({ type: "p", children: [null, "Total: ", 0, false, ["!", ["?"]]] });
// <p>Total: 0!?</p>
JAVASCRIPT
const Icon = (props) => ({ type: "svg", props: { viewBox: "0 0 24 24" }, children: [{ type: "path", props: { d: props.d } }] });
render({ type: Icon, props: { d: "M0 0h24v24" } });
// an <svg> and <path> in the SVG namespace

Environment

  • Plain JavaScript in a browser page; the tests call the global render and inspect the nodes it returns.
  • Updating existing nodes (diffing), refs and HTML inside <foreignObject> are out of scope.
  • Building HTML strings and innerHTML are not allowed: create nodes.

Hints

  1. How does an element deep inside an <svg> know it must be an SVG element?
  2. hidden="false" still hides an element. What does that tell you about boolean props?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…