Crafting Landing Pages: The Peril of Premature Component Abstraction
Building a new landing page with React and Tailwind CSS often feels like a race to create reusable components. Every developer knows the temptation: two similar-looking sections appear, and the urge to abstract them into a single, generic component becomes almost irresistible. But what if that urge is often a trap, leading to more complexity than it solves?
The Rule of Three for UI Components
The 'Rule of Three' isn't just for backend services or utility functions; it's a golden guideline for front-end development too. It posits that you should only consider abstracting a pattern once you've encountered it three times. The first two instances are often too early to fully understand the variations and specific requirements each instance truly demands. What looks like a generic pattern initially might just be two distinct solutions that happen to share some visual similarities, much like two similar-looking puzzle pieces that actually belong to different sections of the puzzle.
A Real Example in landing-page-Econtable
In the context of developing a new landing page for the tadeo14/landing-page-Econtable project, specifically the initial 'home page' feature, we encountered a common scenario. We needed a 'Features' section and a 'Testimonials' section. Both sections presented content in visually distinct cards, but shared a foundational card-like structure.
Initially, one might consider creating a generic Card component to encapsulate the shared visual styling:
// Initial thought: A generic Card component
// (Often becomes bloated with props)
function GenericCard({ title, description, icon, imageUrl, children, customStyles }) {
return (
<div className={`p-6 rounded-lg shadow-md bg-white ${customStyles}`}>
{icon && <div className="mb-4">{icon}</div>}
{imageUrl && <img src={imageUrl} alt="" className="w-12 h-12 rounded-full mb-4" />}
{title && <h3 className="text-xl font-semibold mb-2">{title}</h3>}
{description && <p className="text-gray-600">{description}</p>}
{children}
</div>
);
}
This GenericCard attempts to handle all possible variations. However, the 'Features' cards needed specific icon rendering and simpler descriptions, while 'Testimonials' required user avatars, names, roles, and star ratings – a significantly different internal structure and data model. Trying to force both into GenericCard with numerous conditional props (if icon exists, render this, if imageUrl exists, render that) quickly makes the component hard to read, maintain, and extend.
Instead, maintaining two separate, specific components proved more agile and adaptable to evolving requirements:
// FeatureCard.jsx
function FeatureCard({ icon: Icon, title, description }) {
return (
<div className="p-6 rounded-lg shadow-md bg-white text-center">
<div className="text-4xl text-blue-600 mb-4 flex justify-center">{Icon && <Icon />}</div>
<h3 className="text-xl font-semibold mb-2">{title}</h3>
<p className="text-gray-600">{description}</p>
</div>
);
}
// TestimonialCard.jsx
function TestimonialCard({ avatar, name, role, quote, rating }) {
return (
<div className="p-6 rounded-lg shadow-md bg-white">
<div className="flex items-center mb-4">
<img src={avatar} alt={name} className="w-12 h-12 rounded-full mr-4" />
<div>
<h4 className="font-semibold">{name}</h4>
<p className="text-sm text-gray-500">{role}</p>
</div>
</div>
<p className="italic text-gray-700 mb-4">"{quote}"</p>
{/* Render rating stars here */}
</div>
);
}
These components, while sharing some outer styling principles via Tailwind CSS, are fundamentally different in their internal logic and prop requirements. Each can evolve independently without impacting the other, making changes or new features much easier to implement.
The Lesson
Duplication, especially in the early stages of a UI project, is often a feature, not a bug. It allows each component to develop its unique identity and requirements without the overhead of an overly complex, prematurely abstracted parent. Think of it as allowing individual saplings to grow strong before attempting to prune them into a single, complex bonsai. Wait for a clear, undeniable pattern to emerge – one where the shared logic and props far outweigh the divergent ones, and ideally, where you have at least three distinct instances reinforcing that pattern. Your landing-page-Econtable (or any project) will thank you for the clarity and flexibility.
Generated with Gitvlg.com