liveBook · Manning

Prop

As explained in Essential TypeScript 5, Third Edition

Props are input values supplied to a user-interface component or class to configure the content or behavior it renders.

Props are typed input values that configure what a JSX component renders or how it behaves. A component stores them in a props object, where each property can provide display data, configuration, or a callback for user events. section 16.7.1 section 16.8.1

Typical prop definitions include:

  • Domain data, such as product, order, products, categories, or selectedCategory. section 16.8.1 section 17.3.1
  • Callbacks, such as callback, submitCallback, addToOrderCallback, or filterCallback, invoked by buttons and other event handlers. section 16.8.1 section 21.3
  • DOM property names rather than matching HTML attribute names; for example, JSX uses className instead of class. section 16.7.1

Product display and filtering

A ProductItem can receive a Product and a callback typed as (product: Product, quantity: number) => void. A CategoryList can receive category names, the selected category, and a callback that receives the clicked category. JSX then reads this.props.product and invokes this.props.callback from an onclick handler. section 16.8.1

Listing 16.31 The contents of the productItem.tsx file in the src folder
import { createElement } from "./tools/jsxFactory";
import { Product } from "./data/entities";

export class ProductItem {
   private quantity: number = 1;

   props: {
       product: Product,
       callback: (product: Product, quantity: number) => void
   }

   getContent(): HTMLElement {
       return <div className="card card-outline-primary m-1 p-1 bg-light">
           <h4>
               { this.props.product.name }
               <span className="badge rounded-pill bg-primary text-white"
                       style="float:right">
                   <small>${ this.props.product.price.toFixed(2) }</small>
               </span>
           </h4>
           <div className="card-text bg-white p-1">
               { this.props.product.description }
               <button className="btn btn-success btn-sm float-end"
                       onclick={ this.handleAddToCart } >
                   Add To Cart
               </button>
               <select className="form-control-inline float-end m-1" 
                       onchange={ this.handleQuantityChange }>
                   <option>1</option>
                   <option>2</option>
                   <option>3</option>
               </select>
           </div>
       </div>
   }

   handleQuantityChange = (ev: Event): void => {
       this.quantity = Number((ev.target as HTMLSelectElement).value);
   }

   handleAddToCart = (): void => {
       this.props.callback(this.props.product, this.quantity);
   }
}

Inspect the ProductItem implementation to see a complete props declaration connected to JSX rendering, product data, quantity selection, and an add-to-order callback. section 16.8.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Defines typed data, callback, and optional callback props, but does not descend into a general component-state model.
Examples
Uses ProductItem, CategoryList, ProductList, Header, and OrderDetails to pass products, orders, selections, categories, and callbacks.
What it assumes
Assumes familiarity with TypeScript types, JSX syntax, DOM properties, and DOM event handling.
What you can do after
Apply it via TypeScript JSX by declaring component prop types and passing data or callbacks into rendered components.
How it connects
Connects JSX, DOM API properties, HTML attributes, event handling, callbacks, and React function components.

Also explained in

Compare how 5 books explain prop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.