What are Props in React?
- Props (short for properties) are used to pass data from parent components to child components in React.
- They are read-only and immutable, meaning the child component cannot modify the props directly.
- Props allow components to be dynamic and reusable.
Why Use Props?
- Data Sharing: To share data between components (especially parent to child).
- Dynamic UI: To render dynamic content based on data passed from a parent component.
- Reusable Components: Create components that can handle different data inputs without rewriting the component logic.
How to Use Props in React?
Step-by-Step Explanation:
1. Passing Props
Props are passed to a child component as attributes in the parent component.
function ParentComponent() {
return <ChildComponent name="Dampi" age={18} />;
}
In this example:
name and age are props passed to the ChildComponent.
2. Receiving Props in the Child Component