DEV Community

Marvin Roque
Marvin Roque

Posted on

React Props Explained Simply: Passing Data Between Components

Hey there! 👋 back with another React post. Today, let's talk about props - they're just like passing notes between components!

What Are Props?

Props are React's way of sending data from one component to another. Think of them like passing arguments to a function, but for components.

Here's a simple example:

function Welcome({ name }) {
  return <h1>Hey, {name}!</h1>;
}

// Using it:
function App() {
  return (
    <div>
      <Welcome name="React Friend" />
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Making Components Flexible

Let's make something more useful - a simple card component:

function Card({ title, description, buttonText }) {
  return (
    <div className="card">
      <h2>{title}</h2>
      <p>{description}</p>
      <button>{buttonText}</button>
    </div>
  );
}

// Using our card:
<Card 
  title="My First Card" 
  description="This is awesome!" 
  buttonText="Click me"
/>
Enter fullscreen mode Exit fullscreen mode

Default Props

Sometimes people might forget to pass props. We can handle that:

function Card({ title = "Default Title", description = "No description provided" }) {
  return (
    <div className="card">
      <h2>{title}</h2>
      <p>{description}</p>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

Cool Patterns I Use

1. Object Props

When you have lots of props:

function UserCard({ user }) {
  return (
    <div>
      <h2>{user.name}</h2>
      <p>{user.email}</p>
    </div>
  );
}

// Using it:
<UserCard user={{ name: "Alex", email: "alex@email.com" }} />
Enter fullscreen mode Exit fullscreen mode

2. Children Props

For flexible content:

function Container({ children }) {
  return (
    <div className="container">
      {children}
    </div>
  );
}

// Using it:
<Container>
  <h1>Any content here!</h1>
  <p>Works great!</p>
</Container>
Enter fullscreen mode Exit fullscreen mode

Quick Tips!

  • Props are read-only - don't try to change them!
  • Use clear prop names that make sense
  • Keep your components simple
  • Default props save you from errors

That's it for props! Not so scary after all, right?

Drop a comment if you make something cool with these patterns! 🚀


Found this helpful? Follow for more React tips!

Top comments (0)