liveBook · Manning

Prop

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

Component input values passed from a parent component to a child component.

A prop is data a component receives from outside itself. When a view is split into smaller components, the parent passes each child the values it needs. In the simplest version, components are functions that generate virtual DOM, and their arguments are props. section 3.8.3

Pass a value the child cannot know

A to-do list knows each item's position, but an individual item does not. The list calls `TodoItem({ todo, i, edit }, emit)`, passing the index `i` as a prop. The child can then use that index when an edit or remove event needs to identify the item. The index is relevant to the child without being part of the application's state. section 6.3.3

In a stateful component, keep two responsibilities distinct: `props` holds data received from the parent; `state` holds data maintained internally. A constructor can store incoming props and pass them to `state(props)` to choose an instance's initial state. For example, a counter can initialize its count from `props.initialCount`, falling back to zero. section 9.1.1 section 9.3

Do not change a component's `props` property directly: the component would not know to update its view. Instead, `updateProps(props)` merges incoming values into the current props and patches the component's view. When a parent changes, virtual DOM patching extracts a child's new props, calls that child's `updateProps()`, and retains the component instance. section 9.1.1 section 11.1.4

Props carry data from parent to child; events provide the return path. A child can emit an event that its parent subscribes to. Even the root component can receive props: `createApp(RootComponent, props)` uses them when creating its component virtual node. section 11.0 section 12.4

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

How deep it goes
Implements constructor storage, initialization from props, and re-rendering through updateProps() and virtual DOM patching.
Examples
Uses a to-do item's index and a counter's initial count to show props passed to children and constructors.
What it assumes
Starts with JavaScript function arguments as props, then extends the idea to components with their own state.
What you can do after
Build it from scratch by storing incoming props, passing them to subcomponents, and patching a child's view when they change.
How it connects
Connects components, state, virtual DOM patching, and events as distinct parts of component communication.

Also explained in

Compare how 5 books explain prop →

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.