#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Components

Passing Data through Props

Props, short for properties, are the values a parent component passes to a child component, much like arguments passed to a function. You use them whenever the same component should show different data, for example one ProductCard component rendering many different products.

Sending and receiving props

You pass props as attributes in JSX. Strings can go in quotes; everything else, such as numbers, arrays, objects or functions, goes inside curly braces. The child receives all of them as a single object, which is usually destructured right in the parameter list.

function ProductCard({ name, price, inStock = true }) {
  return (
    <div className="card">
      <h3>{name}</h3>
      <p>Price: {price.toFixed(2)}</p>
      {!inStock && <p className="muted">Out of stock</p>}
    </div>
  );
}

function ProductList() {
  const products = [
    { id: 1, name: 'Keyboard', price: 899 },
    { id: 2, name: 'Mouse', price: 349, inStock: false },
  ];

  return (
    <section>
      {products.map((p) => (
        <ProductCard key={p.id} name={p.name} price={p.price} inStock={p.inStock} />
      ))}
    </section>
  );
}

The = true in the destructuring is a default value. When the parent leaves inStock out, the child treats the product as available.

Props flow one way

Data in React moves downward: parent to child. A child should treat its props as read-only. If ProductCard did props.price = 0, it would be changing data it does not own, and React would not know to re-render anything.

When the child needs to change something, the parent passes a function as a prop, and the child calls it. For example, the parent passes onAddToCart, the child's button calls onAddToCart(id), and the parent updates its own state. The new state then flows back down as fresh props.

Useful prop patterns

  • Boolean shorthand. Writing <Button disabled /> is the same as disabled={true}.
  • Spreading an object. <ProductCard {...p} /> passes every key of p as a separate prop. It is convenient but can pass more than the child needs, so use it with care.
  • The children prop. Anything placed between a component's opening and closing tags arrives as children, which is how wrapper components like Card or Modal work.

Mistakes that trip people up

Quoting a number is a frequent one. price="899" passes a string, so price.toFixed(2) throws an error. Use price={899} to pass a real number.

Another is forgetting that key is special. React uses it to track list items and does not pass it to the component, so props.key is always undefined. If the child needs the id, pass it again under another name, like id={p.id}.

Finally, watch for misspelled prop names. If the parent sends productName but the child reads name, nothing errors; the value is simply undefined and the heading is blank.

When props get passed too deep

Sometimes a value has to travel through several components that do not use it, just to reach a deeply nested child. This is called prop drilling. For a few levels it is perfectly fine. When it becomes painful, React's Context or a state library can make the value available directly, which you will meet later.

Main idea: props are inputs. Parents decide them, children read them, and changes go back up through callback props.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course