Loading Depth Study
Preparing your workspace
Conditional rendering means displaying different React elements depending on the current application data.
React uses normal JavaScript conditions to decide what should appear. Ternary expressions are useful for choosing between two UI branches. Logical && is useful when something should appear only when a condition is true. For larger conditions, regular if statements can be clearer. Conditional rendering is essential for loading states, authentication, permissions, empty states, error messages, and feature states.
A student dashboard may show 'Loading...' while data is being fetched, a course list after data arrives, or an error message when the request fails.
function CourseStatus({ loading, error, courses }) {
if (loading) {
return <p>Loading courses...</p>;
}
if (error) {
return <p>Unable to load courses.</p>;
}
if (courses.length === 0) {
return <p>No courses found.</p>;
}
return <p>{courses.length} courses available.</p>;
}The component displays a different message depending on loading, error, empty, or successful data states.