Working with Lists, Forms, and Conditional Rendering in React
Share
After learning components, props, and state, many React students begin working with three important interface patterns: lists, forms, and conditional rendering. These topics often appear together because many interfaces need to display collections of data, receive input from users, and show different content depending on current values. Understanding how these patterns connect helps students move from small examples to more complete learning interfaces.
Lists are common in React because interfaces often display repeated information. A course website might show course cards. A dashboard might show tasks. A catalog might show items. Instead of writing each item manually, React allows developers to store information in an array and render it using a mapping function.
A simple list example looks like this:
const courses = [
{ id: 1, title: "Components", topic: "Structure" },
{ id: 2, title: "State", topic: "Interaction" },
{ id: 3, title: "Forms", topic: "Input" }
];
function CourseList() {
return (
<div>
{courses.map((course) => (
<div key={course.id} className="course-card">
<h3>{course.title}</h3>
<p>{course.topic}</p>
</div>
))}
</div>
);
}
The map method turns each item in the array into a piece of interface. Each rendered item needs a key, which helps React identify the item in the list. In this example, course.id is used as the key. This is a common pattern when working with data.
Lists become more useful when combined with reusable components. Instead of placing all card code inside the list, we can create a separate card component:
function CourseCard({ course }) {
return (
<div className="course-card">
<h3>{course.title}</h3>
<p>{course.topic}</p>
</div>
);
}
function CourseList() {
return (
<div>
{courses.map((course) => (
<CourseCard key={course.id} course={course} />
))}
</div>
);
}
This makes the structure cleaner. CourseList focuses on rendering a collection, while CourseCard focuses on displaying one item. This separation helps keep the code readable as the interface grows.
Forms are another important part of React interfaces. They allow users to enter text, select options, or submit information. In React, form fields are often connected to state. This means the component remembers the current input value and updates the interface when the value changes.
A simple input example looks like this:
import { useState } from "react";
function SearchBox() {
const [searchText, setSearchText] = useState("");
return (
<div>
<input
value={searchText}
onChange={(event) => setSearchText(event.target.value)}
placeholder="Search topics"
/>
<p>Search value: {searchText}</p>
</div>
);
}
The input value is controlled by React state. When the user types, onChange updates the state. The paragraph displays the same value. This pattern is useful because it makes the input value available for other logic, such as filtering a list.
For example, a search field can filter a list of courses:
function FilteredCourseList() {
const [searchText, setSearchText] = useState("");
const filteredCourses = courses.filter((course) =>
course.title.toLowerCase().includes(searchText.toLowerCase())
);
return (
<div>
<input
value={searchText}
onChange={(event) => setSearchText(event.target.value)}
placeholder="Search courses"
/>
{filteredCourses.map((course) => (
<CourseCard key={course.id} course={course} />
))}
</div>
);
}
This example shows how lists, forms, and state connect. The form input changes searchText. The filtered list is calculated from that value. The interface displays only the matching items. This is a practical React pattern that appears in many learning projects.
Conditional rendering adds another layer. Sometimes an interface should show one block in one situation and another block in a different situation. For example, if no courses match the search value, the interface can show a message:
{filteredCourses.length > 0 ? (
filteredCourses.map((course) => (
<CourseCard key={course.id} course={course} />
))
) : (
<p>No matching courses found.</p>
)}
This condition checks the length of the filtered list. If there are items, it renders the cards. If there are no items, it shows a message. Conditional rendering makes interfaces more informative because the screen can respond to different states of the data.
Students sometimes find these topics difficult because they involve several ideas at once. A form updates state. State affects a list. The list may show different content depending on a condition. The key is to trace the flow step by step. First, identify the state value. Then identify what changes it. Next, identify what depends on it. Finally, decide what should appear on the screen.
A helpful learning exercise is to build a topic library. Start with an array of topic objects. Display them as cards. Add a search input. Use state to store the search value. Filter the list. Add a message for an empty result. Then add a selected card state so clicking a card shows more details. This small project includes components, props, state, events, lists, forms, and conditional rendering in one structured example.
The goal is not to write a large application right away. The goal is to understand how React connects data and interface behavior. Lists show repeated data. Forms collect changing input. Conditional rendering decides what appears. Together, these patterns help students build interfaces that feel organized and responsive to current values.
By practicing these topics together, students begin to read React code more clearly. They can see where data starts, how it changes, which components display it, and why the interface shows one result instead of another. This understanding is an important step in learning React programming in a structured way.