DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Passing Props in React (How Components Talk to Each Other)

Why Passing Props Exists

In React:

  • Components are isolated
  • One component cannot access another’s data directly

So how do they communicate?

πŸ‘‰ Props


What Are Props?

Props are inputs to a component.

Think of them like:

  • Function parameters
  • API request data

Simple Example

function Greeting({ name }) {
  return <h1>Hello {name}</h1>;
}

function App() {
  return <Greeting name="John" />;
}
Enter fullscreen mode Exit fullscreen mode

What’s Happening?

  • App sends data β†’ name="John"
  • Greeting receives it β†’ { name }
  • UI updates based on props

Backend analogy:

function greeting(name) {
  return `Hello ${name}`;
}

greeting("John");
Enter fullscreen mode Exit fullscreen mode

Props Are Read-Only

❌ This is NOT allowed:

props.name = "Mike";
Enter fullscreen mode Exit fullscreen mode

Props are:

  • Immutable
  • Owned by the parent

If data must change β†’ it belongs in state, not props.


Common Beginner Mistake

Trying to store everything in one component.

Correct mindset:

Components should receive data, not own everything.


Key Takeaway

Props are how data flows downward in React.

No props β†’ no real app.

Top comments (0)