π React JS Example β Students, Courses, and Marks using Parent & Child Components
In React JS, we can divide our UI into multiple components and establish a clear communication between them using props. In this blog, weβll build a basic structure where a Student is the parent, and the Course and Marks are child components.
π What You Will Learn
- How to pass data from Parent to Child using
props
- How to use arrays and map through them
- How to break UI into reusable components
π§© Component Structure
src/
βββ components/
β βββ Student.jsx # Parent component
β βββ CourseList.jsx # Child: Displays course and marks
β βββ CourseItem.jsx # Child: Single course item
βββ App.jsx
π§βπ Parent Component: Student.jsx
import React from 'react';
import CourseList from './CourseList';
const Student = () => {
const studentData = {
name: "Anjum Khan",
courses: [
{ title: "Mathematics", marks: 92 },
{ title: "Physics", marks: 85 },
{ title: "Computer Science", marks: 98 }
]
};
return (
<div>
<h2>Student Name: {studentData.name}</h2>
<CourseList courses={studentData.courses} />
</div>
);
};
export default Student;
π Child Component: CourseList.jsx
import React from 'react';
import CourseItem from './CourseItem';
const CourseList = ({ courses }) => {
return (
<div>
<h3>Courses & Marks:</h3>
<ul>
{courses.map((course, index) => (
<CourseItem key={index} title={course.title} marks={course.marks} />
))}
</ul>
</div>
);
};
export default CourseList;
π Child Component: CourseItem.jsx
import React from 'react';
const CourseItem = ({ title, marks }) => {
return (
<li>
<strong>{title}</strong> β Marks: {marks}
</li>
);
};
export default CourseItem;
π§ App.jsx β Using the Student Component
import React from 'react';
import Student from './components/Student';
function App() {
return (
<div className="App">
<h1>React Student Dashboard</h1>
<Student />
</div>
);
}
export default App;
π Output Example
When you run this app, it will display:
- Student Name: Anjum Khan
- Courses & Marks:
- Mathematics β Marks: 92
- Physics β Marks: 85
- Computer Science β Marks: 98
π‘ Key Concepts Covered
- Props: Used to pass data from parent to child
- Reusable components: CourseItem can be reused across different students
- Array.map(): Render a list of components dynamically
π Final Thoughts
This example helps beginners understand how React components work together. As you progress, you can enhance this by adding forms to input marks, managing state with useState
, and storing data using useContext
or external APIs.
Up next: βHow to Handle Events and useState in Reactβ