Skip to content
Core ConceptsChapter 6 / 37Beginner4 min read

Props

Learn what React props are, how data flows from a parent to a child component, and how to use destructuring and default values, with examples.

On this page

1. What are props?

In React, a component is a function. Just as you give arguments to a function, you give props to a component. The full name of props is "properties".

With props, the same component can be reused with different data. One ProductCard component, a hundred products.

2. Why do they matter?

Without props, you would have to write a separate component for every product. Props make a component reusable.

Without propsWith props
A separate component for every cardOne component, data comes from outside
Text hardcoded inside the componentThe parent decides the text
Edit every place to change somethingJust change the data, the UI updates itself
Copy-pasted codeClean and reusable code

3. Where are they used?

Use caseJob of props
Product cardPass name, price, image
ButtonPass label, variant, onClick
User avatarPass src, size, alt
LayoutPass title and children

4. How do they work?

The parent writes attributes in JSX. React collects all of them into one object and gives it to the child function as the first argument.

The journey of props
The parent writes attributes in JSX
React turns all the attributes into one props object
The child function receives the props object
The child returns the UI based on the props

Data goes only from top to bottom (parent to child). This is called one-way data flow.

5. Code example

Basic props

jsx
function Greeting(props) {
  return <h2>Hello, {props.name}!</h2>;
}

export default function App() {
  return (
    <>
      <Greeting name="Ali" />
      <Greeting name="Sara" />
    </>
  );
}

Destructuring and default values

jsx
function ProductCard({ name, price, inStock = true }) {
  return (
    <div className="card">
      <h3>{name}</h3>
      <p>Rs. {price}</p>
      <p>{inStock ? "Available" : "Out of stock"}</p>
    </div>
  );
}

export default function App() {
  return (
    <>
      <ProductCard name="Keyboard" price={4500} />
      <ProductCard name="Mouse" price={1800} inStock={false} />
    </>
  );
}

How does the code work?

ItemMeaning
name="Ali"A string prop, in quotes
price={4500}A number or any JS value, in curly braces
{ name, price }Destructuring the props object directly
inStock = trueA default value, used when the parent did not pass the prop
inStock={false}A boolean prop explicitly set to false

Apart from strings, every value (number, boolean, array, object, function) is passed inside {}.

6. Common mistakes

Mistake: Changing props inside the child component

Why? Props are read-only. Mutating them puts the UI and the data out of sync and causes bugs.

Better: If a value needs to change, keep it as state in the parent and give the child a function prop that updates the parent's state.

Mistake: Passing a number in quotes, like turning price into a string

Why? A value in quotes is always a string, so math or comparisons give wrong results.

Better: Pass numbers, booleans and objects in curly braces, like price={4500}.

Mistake: Forgetting the curly braces in destructuring

Why? Writing function Card(name) makes name the whole props object, not just the name.

Better: Write function Card({ name }) so that only name is taken from the props object.

Interview tip

If you are asked "What is the difference between props and state?", the answer is: props come from outside and are read-only, while state is the component's own memory that it updates itself.

7. What to learn before and after

Final quick revision

PointDetail
What props areInputs given from a parent to a child
ShapeAn object, the first function argument
DirectionOnly top to bottom (one-way)
RuleA child does not change its props
Default value= value in destructuring