Today I spent some time revisiting fundamental React concepts to make my components cleaner, more declarative, and easier to maintain.
🧠 Concepts I Focused On:
- ✅ Basic Rendering
- ⚡ Conditional Rendering (
&&
and? :
) - 🔁 Multiple Returns
- 🕒 Conditional UI based on time logic
🧩 A Few Code Snippets:
1. Conditional Rendering with &&
{isOpen && (
<div className="order">
<p>We're open until {closeHour}:00. Come visit us or order online.</p>
<button className="btn">Order</button>
</div>
)}
👉 Renders this block only if isOpen
is true
.
2. Ternary Operator for Dynamic UI
{numPizzas > 0 ? (
<ul>{/* pizza list here */}</ul>
) : (
<p>We're still working on our menu.</p>
)}
👉 Keeps the UI simple and declarative without extra if
statements.
3. Multiple Returns for Cleaner Components
if (soldOut) return null;
return <li className="pizza">{/* pizza UI */}</li>;
👉 Returning early makes components easier to read and avoids unnecessary nesting.
🚀 Key Takeaways:
- Clean rendering logic improves readability.
- Conditional rendering is powerful when used smartly.
- Multiple returns keep components lean and focused.
- Declarative UI is easier to maintain as projects grow.
💬 Next Step: I’ll be expanding this by adding interactive features and improving state management.
Top comments (0)