Props Introduction in React
Learn how React components receive data through props and display reusable content at CIIT Training Institute.
01 Learning Objectives
After completing this lesson, you will be able to:
Learn how props pass data to components.
Display different information with the same component.
Pass strings, numbers and other supported values.
Understand why props should be treated as read-only.
02 What Are Props?
Props means properties. They allow a parent component to pass data to a child component. The child component receives this data and uses it to display dynamic content.
function Welcome(props) {
return <h2>Welcome, {props.name}!</h2>;
}
export default Welcome;
import Welcome from "./Welcome";
function App() {
return (
<div>
<Welcome name="Samadhan" />
</div>
);
}
export default App;
Welcome, Samadhan!
03 How Props Work
The parent component supplies values through JSX attributes. React makes those values available to the child component through its props parameter.
name="Samiksha"
props.name
function Welcome(props) {
return <h2>Welcome, {props.name}!</h2>;
}
function App() {
return <Welcome name="Samiksha" />;
}
export default App;
Welcome, Samiksha!
The value of the name prop is displayed by Welcome.
04 Passing String Props
String values can be passed through JSX attributes using quotation marks. The same component can display different messages depending on the value it receives.
function Greeting(props) {
return <p>Hello, {props.name}!</p>;
}
export default Greeting;
import Greeting from "./Greeting";
function App() {
return (
<section>
<h2>CIIT Student Greetings</h2>
<Greeting name="Samadhan" />
<Greeting name="Samiksha" />
<Greeting name="Pradnya" />
</section>
);
}
export default App;
CIIT Student Greetings
Hello, Samadhan!
Hello, Samiksha!
Hello, Pradnya!
05 Passing Number Props
To pass a number or another JavaScript expression, use curly braces in JSX. Without curly braces, a value written inside quotation marks is treated as a string.
function Student(props) {
return (
<div>
<h3>{props.name}</h3>
<p>Marks: {props.marks}</p>
<p>Next Test Marks: {props.marks + 5}</p>
</div>
);
}
export default Student;
import Student from "./Student";
function App() {
return (
<Student name="Pradnya" marks={80} />
);
}
export default App;
Pradnya
Marks: 80
Next Test Marks: 85
marks={80} passes the number 80, whereas
marks="80" passes the string "80".
06 Passing Multiple Props
A component can receive multiple props at the same time. For example, a course card can receive a course name, duration and training mode.
function CourseCard(props) {
return (
<div>
<h3>{props.courseName}</h3>
<p>Duration: {props.duration}</p>
<p>Mode: {props.mode}</p>
</div>
);
}
export default CourseCard;
import CourseCard from "./CourseCard";
function App() {
return (
<CourseCard
courseName="React JS"
duration="3 Months"
mode="Online and Offline"
/>
);
}
export default App;
React JS
Duration: 3 Months
Mode: Online and Offline
07 Reusing Components with Different Props
One reusable component can render multiple cards with different data. The component structure remains the same, while its props determine the content displayed.
function CourseCard(props) {
return (
<div>
<h3>{props.title}</h3>
<p>Technology: {props.technology}</p>
</div>
);
}
export default CourseCard;
import CourseCard from "./CourseCard";
function App() {
return (
<section>
<h2>CIIT Training Courses</h2>
<CourseCard
title="React Development"
technology="React JS"
/>
<CourseCard
title=".NET Full Stack"
technology="ASP.NET Core"
/>
<CourseCard
title="Python Development"
technology="Python"
/>
</section>
);
}
export default App;
CIIT Training Courses
08 Props Are Read-Only
Props should be treated as read-only inputs. A component must not assign a new value directly to a prop. To change displayed information, the parent can pass new props or use state and event handlers.
function Welcome(props) {
// Read the prop; do not modify it.
return <h2>Welcome, {props.name}!</h2>;
}
export default Welcome;
props.name = "Another Name" inside
the component. Instead, update the value in the parent and
pass the new value down.
Welcome, Samadhan!
The component displays the value provided by its parent.
09 Common Props Mistakes
1. Missing Props Parameter
If a component needs a prop, receive it through the component parameter.
function Welcome(props) { ... }
2. Incorrect JavaScript Value Syntax
Use curly braces when passing a number or JavaScript expression.
<Student marks={80} />
3. Wrong Prop Name
The name used by the child must match the attribute passed by the parent.
<Welcome name="Samiksha" />
4. Modifying Props
Treat props as read-only. Use parent updates or state for changing data.
return <p>{props.name}</p>;
10 Practice Questions
- What are props in React?
- How does a parent component pass props to a child?
- Pass a name prop to a Welcome component.
- Create a Student component that receives name and marks.
- Build a CourseCard that receives course name and duration.
- Display Samadhan, Samiksha and Pradnya using one reusable component.
- Why should props be treated as read-only?
Summary
Props are inputs that allow parent components to pass data to child components. They can contain strings, numbers and other JavaScript values. A reusable component can display different content depending on the props it receives. Props should be treated as read-only, and component reuse helps keep applications organized and maintainable at CIIT Training Institute.