liveBook · Manning

extractPropsAndEvents

As explained in Build a Frontend Web Framework (From Scratch)

A book-specific utility function that separates component properties from event handlers and removes the key property from the properties passed to a component.

extractPropsAndEvents separates a virtual DOM node’s component properties from its event handlers. It also keeps framework-only data out of component and DOM properties, avoiding duplicated extraction logic during mounting and patching. section 11.2.2 section 12.1.3

Listing 11.10 Extracting the props and events for a component (utils/props.js)
export function extractPropsAndEvents(vdom) {
  const { on: events = {}, ...props } = vdom.props

  return { props, events }
}

The core implementation uses destructuring: the on property becomes events, the remaining properties become props, and missing handlers produce an empty object. section 11.2.2

The utility has three small responsibilities:

  • Extract the on property as event handlers, defaulting to an empty object. section 11.2.2
  • Return the remaining properties separately for component construction or DOM attribute setting. section 11.2.2 section 12.2
  • Delete key from the copied props object while leaving the virtual DOM node’s original props available to reconciliation. section 12.1.3

Keyed nodes without leaking key

A keyed list can use key to detect moved elements during reconciliation, while extractPropsAndEvents removes key before attributes reach a DOM element or component instance. Event handlers are attached separately. section 12.2 section 11.2.2

Listing 12.3 Removing the key from the props object (utils/props.js)
export function extractPropsAndEvents(vdom) {
  const { on: events = {}, ...props } = vdom.props
  delete props.key  #1

  return { props, events }
}

The extended implementation deletes props.key after the rest operation. This removes only the returned copy, preserving key on the virtual DOM node for node comparison. section 12.1.3

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Defines extraction with destructuring and spread, then extends it to delete key while preserving virtual DOM data.
Examples
Uses component mounting, component patching, keyed element movement, and modified DOM property handling as implementation examples.
What it assumes
Assumes virtual DOM props and component mounting, then teaches separation and framework-key filtering directly.
What you can do after
Build it from scratch by extracting on, returning props and events, and deleting key from the copied props object.
How it connects
Connects virtual DOM nodes, component mounting, event handlers, keyed lists, reconciliation, and DOM attribute setting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.