Avoid 'use client' at top of layout for better React performance

𝗠𝗼𝘀𝘁 𝗥𝗲𝗮𝗰𝘁 𝗱𝗲𝘃𝘀 𝗺𝗶𝘀𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 'use client'. It doesn't make one component a Client Component. It makes the entire subtree one. ━━━━━━━━━━━━━━━━━━━━━━━ The whiteboard above shows the what. Here's the part that actually matters in production: 𝗪𝗵𝗲𝗿𝗲 𝘆𝗼𝘂 𝗽𝗹𝗮𝗰𝗲 'use client' is an architecture decision. ⛔ The mistake 90% of devs make: 'use client' // ← at the top of a layout or page wrapper export default function ProductPage() {  return (   <div>    <ProductList />  {/* now a Client Component — ships JS */}    <Filters />    {/* now a Client Component — ships JS */}    <Pagination />  {/* now a Client Component — ships JS */}   </div>  ) } Every child inherits the client boundary. Your bundle grows. Your initial load slows. You just opted your entire page out of SSR. ━━━━━━━━━━━━━━━━━━━━━━━ ✅ The right mental model: Server Components = data shells (fetch, render, done) Client Components = interactive islands (state, events, effects) Keep 'use client' as deep and as small as possible. async function ProductPage() {       // Server — fetches data  const products = await fetchProducts()  return (   <ProductList products={products}>    // Server — renders HTML    <AddToCartButton />          {/* Client — needs onClick */}   </ProductList>  ) } Only AddToCartButton ships JavaScript to the browser. Everything else is static HTML. Zero bundle impact. ━━━━━━━━━━━━━━━━━━━━━━━ 𝗧𝗵𝗲 𝗱𝗲𝗰𝗶𝘀𝗶𝗼𝗻 𝗳𝗿𝗮𝗺𝗲𝘄𝗼𝗿𝗸 𝗜 𝘂𝘀𝗲: Does it need useState, useEffect, or event handlers? → Client Component Does it fetch data, access the database, or just render? → Server Component Does it need both? → Split it. Server shell, Client leaf. ━━━━━━━━━━━━━━━━━━━━━━━ The whiteboard covers the theory. The 'use client' bubble effect is what trips up production apps. Save this 📌 — and drop a 🔥 if you've made the top-level mistake before. #ReactJS #FrontendDevelopment #JavaScript #NextJS #WebPerformance #OpenToWork #ImmediateJoiner

  • timeline

To view or add a comment, sign in

Explore content categories