Skip to main content

Components

1. Keep Components Pure​

Components should be pure functions that only depend on their props and lens:

Pure Components
// 
const UserCard = createComponent(({ lens, user, onEdit }) => {
const isExpanded = lens.useRefraction(false);

return (
<div className="user-card">
<h3>{user.name}</h3>
<button onClick={() => isExpanded.set(!isExpanded.value)}>
{isExpanded.value ? 'Collapse' : 'Expand'}
</button>
{isExpanded.value && (
<div>
<p>{user.email}</p>
<button onClick={() => onEdit(user)}>Edit</button>
</div>
)}
</div>
);
});

2. Use Descriptive Names​

Choose clear, descriptive names for your components:

Descriptive Names
// ✅ Good - descriptive names
const UserProfileCard = createComponent(({ lens, user }) => { /* ... */ });
const ShoppingCartSummary = createComponent(({ lens, items }) => { /* ... */ });
const NavigationMenu = createComponent(({ lens, items }) => { /* ... */ });

3. Organize Props Interface​

Structure your props for clarity and maintainability:

Clear Prop Structure
// ✅ Good - clear prop structure
const ProductCard = createComponent(({
lens,
product, // Main data
showPrice = true, // Configuration
onAddToCart, // Event handlers
onViewDetails
}) => {
// Component logic
});