liveBook · Manning

updateProps

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

A component method that updates the component's props and triggers its re-rendering.

updateProps is the controlled way for a component to accept new props. It merges those props into the existing props object, then patches the component so its view reflects the change. section 11.0 section 9.1.1

The method has two small responsibilities:

  • Merge incoming props with the current props, preserving values that were not replaced. section 11.1.4
  • Call the component's private patching method so the rendered view updates. section 11.1.4
  • Let parent-component patching extract new child props and invoke updateProps on the child. section 11.1.4

Minimal implementation

updateProps(props) { this.props = { ...this.props, ...props } this.#patch() } section 11.1.4

Listing 11.6 Updating the props of a component (component.js)
export function defineComponent({ render, state, ...methods }) {
  class Component {
 // --snip-- //

    updateProps(props) {
      this.props = { ...this.props, ...props }      #1
      this.#patch()                                #2
    }

    updateState(state) {
      this.state = { ...this.state, ...state }
      this.#patch()
    }

 // --snip-- //
 }

 // --snip-- //
}

Inspect the complete updateProps implementation and compare it with updateState, which follows the same merge-then-patch pattern. section 11.1.4

Listing 11.8 Patching a component node (patch-dom.js)
function patchComponent(oldVdom, newVdom) {
  const { component } = oldVdom             #1
  const { props } = newVdom             #2

  component.updateProps(props)              #3

  newVdom.component = component           #4
  newVdom.el = component.firstElement     #5
}

Trace how a patched component virtual node reuses the existing component, passes it new props, and records its component and first DOM element. section 11.1.4

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

How deep it goes
Defines the merge-and-patch implementation and its virtual DOM call path, while deferring optional avoidance of unnecessary re-renders.
Examples
Uses a JavaScript method, component-node patching, and a diagram of state changes passing new props to subcomponents.
What it assumes
Assumes component props, state, rendering, and virtual DOM patching, while treating direct property mutation as insufficient.
What you can do after
Apply the merge-and-patch pattern to implement prop updates and propagate them through component virtual nodes.
How it connects
Connects props with component state, virtual DOM patching, child components, and parent-to-child communication.

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.