"In real life we use couriers to send packages. In React? We use props!"
Hello, fellow React wanderers!
If you’ve started exploring components in React, you’ve likely come across the term props. It might sound technical, but props are actually super simple—they're like messages or packages you send from one component to another.
In this chill yet comprehensive guide, we’ll cover:
- What props are
- Why props matter
- How to use them
- The difference between props and state
- Real-world examples
- Best practices
What Are Props?
Props is short for properties.
In React, props allow you to send data from a parent component to a child component. Imagine props as care packages that travel from one React house (component) to another.
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
function App() {
return <Greeting name="Alex" />;
}
Explanation:
Greeting
is the child componentApp
is the parentname="Alex"
is the prop{props.name}
reads the prop value
Why Are Props Important?
Props are React's way of letting components communicate. Without props, our components would be isolated islands. With props, we can build dynamic, reusable, and powerful interfaces.
Benefits of using props:
- Components become reusable
- Data flows clearly from top to bottom
- UIs can be dynamically controlled
A More Complete Example
function Profile(props) {
return (
<div>
<img src={props.avatar} alt="Profile" />
<h2>{props.name}</h2>
<p>{props.bio}</p>
</div>
);
}
function App() {
return (
<Profile
name="Sarah"
avatar="https://via.placeholder.com/150"
bio="Frontend developer who loves cats and coffee."
/>
);
}
Props can be:
- Strings
- Numbers
- Booleans
- Arrays
- Objects
- Functions
- Even components
Props Are Read-Only
One crucial rule: props are immutable. You can’t change them inside the child component. Trying to do so will lead to React scolding you!
Props vs State
Props | State |
---|---|
Received from parent | Managed internally |
Read-only | Can change via setState or useState |
External control | Internal control |
Used to configure components | Used to handle component behavior |
Passing Functions via Props
function Button(props) {
return <button onClick={props.onClick}>{props.label}</button>;
}
function App() {
function handleClick() {
alert("Button clicked!");
}
return <Button label="Click Me" onClick={handleClick} />;
}
Destructuring Props
function Card({ title, content }) {
return (
<div>
<h1>{title}</h1>
<p>{content}</p>
</div>
);
}
Cleaner and easier to read!
Default Values for Props
function Hello({ name = "Guest" }) {
return <h2>Hi, {name}!</h2>;
}
Nested Props
function User({ data }) {
return (
<div>
<h3>{data.name}</h3>
<p>{data.email}</p>
</div>
);
}
const userInfo = { name: "Dina", email: "dina@mail.com" };
function App() {
return <User data={userInfo} />;
}
Using props.children
function Container(props) {
return <div className="box">{props.children}</div>;
}
function App() {
return (
<Container>
<h1>Welcome!</h1>
<p>This is inside the container.</p>
</Container>
);
}
Validating Props
import PropTypes from 'prop-types';
function Profile({ name, age }) {
return <p>{name} - {age} years old</p>;
}
Profile.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number
};
Best Practices with Props
- Use descriptive prop names
- Destructure props to keep code clean
- Never mutate props
- Use default values where necessary
- Use
children
to create flexible layouts
Case Study: Product Card Component
function ProductCard({ title, price, image }) {
return (
<div className="card">
<img src={image} alt={title} />
<h3>{title}</h3>
<p>{price}</p>
</div>
);
}
function App() {
return (
<>
<ProductCard
title="Cool T-Shirt"
price="$25.00"
image="https://via.placeholder.com/150"
/>
<ProductCard
title="Stylish Hat"
price="$15.00"
image="https://via.placeholder.com/150"
/>
</>
);
}
Props are a foundational part of React. They’re how you make components reusable and how you pass dynamic information through your app. Like a messenger between kingdoms, props help your React components stay in sync.
"Props are like the WhatsApp of React components—no messages, no life!"
0 Comments:
Post a Comment