Props are one of the most fundamental concepts in React. They are the primary way to pass data from a parent component to a child component. In React, components are designed to be reusable, and props make this possible by allowing components to receive different data each time they are used.
At a basic level, props work like function arguments. When you call a function, you pass values to it; similarly, when you use a React component, you pass props to it. These props are then available inside the component.
Basic example of props:
function Greeting(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return <Greeting name="Akbar" />;
}
In this example, the App component passes a name prop to the Greeting component. The Greeting component receives this data through the props object and uses it to render dynamic content.
Props can also be destructured to make code cleaner and easier to read.
Example using destructuring:
function Greeting({ name }) {
return <h1>Hello, {name}</h1>;
}
Props are read-only, meaning a child component should never modify the props it receives. This rule ensures predictable data flow and makes applications easier to debug. If a component needs to change data, that data should be managed by state in the parent component.
Props are not limited to strings or numbers. You can pass arrays, objects, functions, and even other components as props.
Example passing different types of props:
function Button({ label, onClick }) {
return <button onClick={onClick}>{label}</button>;
}
function App() {
function handleClick() {
alert("Button clicked");
}
return <Button label="Submit" onClick={handleClick} />;
}
Here, a function is passed as a prop, allowing the child component to communicate back to the parent through events. This is a common and powerful pattern in React applications.
A real-world example of props can be seen in form-based applications. A reusable Input component might receive props such as type, value, placeholder, and onChange. The same Input component can then be used for name, email, or password fields by passing different props.
Example:
function Input({ type, placeholder, value, onChange }) {
return (
<input
type={type}
placeholder={placeholder}
value={value}
onChange={onChange}
/>
);
}
Another important concept related to props is children. The children prop allows components to wrap and display nested content.
Example using children:
function Card({ children }) {
return <div className="card">{children}</div>;
}
This makes the Card component reusable for different types of content while keeping a consistent layout.
Some important best practices include keeping props simple, avoiding excessive prop drilling, and clearly naming props to reflect their purpose. When many components need the same data, alternative solutions such as context can be used instead of deeply passing props.
In summary, props are the mechanism through which React components communicate and share data. They enable reusability, maintain a clear data flow, and are essential for building dynamic and scalable React applications.