← React Beginner Questions - Set 1 Q8

What are props in React?

React Beginner Questions - Set 1

Props are the arguments of a component. They flow one way, from parent to child, and the child must not mutate them.

Example

function Price({ amount, currency = 'USD' }) {
  return <strong>{currency} {amount.toFixed(2)}</strong>;
}

function ProductCard({ product, onAdd }) {
  return (
    <div className="card">
      <h3>{product.name}</h3>
      <Price amount={product.price} currency={product.currency} />
      <button onClick={() => onAdd(product.id)}>Add to cart</button>
    </div>
  );
}

// <ProductCard product={lamp} onAdd={addToCart} />

What to notice

← Back to the question list