Next.js App Router: Server and Client Components Explained

𝑵𝒆𝒙𝒕.𝒋𝒔 𝑭𝑨𝑸 2. 𝐖𝐡𝐚𝐭 𝐚𝐫𝐞 𝐒𝐞𝐫𝐯𝐞𝐫 𝐂𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭𝐬 𝐚𝐧𝐝 𝐂𝐥𝐢𝐞𝐧𝐭 𝐂𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭𝐬? (𝐓𝐡𝐞 𝐧𝐞𝐰 𝐀𝐩𝐩 𝐑𝐨𝐮𝐭𝐞𝐫 𝐩𝐚𝐫𝐚𝐝𝐢𝐠𝐦) With the new "App Router," Next.js introduced a powerful concept: components can run either on the server or on the client (the user's browser). 🔸 𝑺𝒆𝒓𝒗𝒆𝒓 𝑪𝒐𝒎𝒑𝒐𝒏𝒆𝒏𝒕𝒔 are the default. They run on the server, can directly access databases or files, and send fully-rendered HTML to the browser. They cannot use browser-only features like useState or useEffect. 🔸 𝑪𝒍𝒊𝒆𝒏𝒕 𝑪𝒐𝒎𝒑𝒐𝒏𝒆𝒏𝒕𝒔 are what you're used to from traditional React. They run in the browser, can use state and effects, and handle user interactions. You opt into them by putting 'use client'; at the top of the file. 𝘼𝙣𝙖𝙡𝙤𝙜𝙮: 𝘽𝙪𝙞𝙡𝙙𝙞𝙣𝙜 𝙖 𝙃𝙤𝙪𝙨𝙚: 𝙎𝙚𝙧𝙫𝙚𝙧 𝘾𝙤𝙢𝙥𝙤𝙣𝙚𝙣𝙩𝙨 𝙖𝙧𝙚 𝙋𝙧𝙚𝙛𝙖𝙗𝙧𝙞𝙘𝙖𝙩𝙚𝙙 𝙒𝙖𝙡𝙡𝙨: These are built in a factory (the server) with everything inside—insulation, framework, even window frames. They are shipped to the construction site (the browser) as complete, solid units. They are strong and efficient but you can't change their internal structure on-site. 𝘾𝙡𝙞𝙚𝙣𝙩 𝘾𝙤𝙢𝙥𝙤𝙣𝙚𝙣𝙩𝙨 𝙖𝙧𝙚 𝙩𝙝𝙚 𝙊𝙣-𝙎𝙞𝙩𝙚 𝙀𝙡𝙚𝙘𝙩𝙧𝙞𝙘𝙖𝙡 𝙒𝙞𝙧𝙞𝙣𝙜: This work must be done on-site (the browser). An electrician (JavaScript) needs to be there to connect switches (onClick), run wires (useState), and make the lights interactive. 𝙒𝙝𝙮 𝙏𝙝𝙞𝙨 𝙎𝙚𝙥𝙖𝙧𝙖𝙩𝙞𝙤𝙣 𝙈𝙖𝙩𝙩𝙚𝙧𝙨: ➛ 𝑷𝒆𝒓𝒇𝒐𝒓𝒎𝒂𝒏𝒄𝒆: By defaulting to Server Components, Next.js reduces JavaScript bundle size and speeds up page loads. Only interactive parts need client-side JavaScript. ➛ 𝑺𝒆𝒄𝒖𝒓𝒊𝒕𝒚: Sensitive logic and data fetching stay on the server, never exposed to the client. ➛ 𝑫𝒆𝒗𝒆𝒍𝒐𝒑𝒆𝒓 𝑬𝒙𝒑𝒆𝒓𝒊𝒆𝒏𝒄𝒆: You write components naturally and only opt into client-side behavior when necessary, simplifying code and improving maintainability. 𝐈𝐧𝐝𝐮𝐬𝐭𝐫𝐲 𝐁𝐞𝐬𝐭 𝐏𝐫𝐚𝐜𝐭𝐢𝐜𝐞: Keep components as Server Components by default for best performance. Only switch to a Client Component ('use client';) when you absolutely need browser-based interactivity (like state, event listeners, or lifecycle effects). You can even nest a Client Component inside a Server Component, getting the best of both worlds. 𝑰𝒏 𝒔𝒖𝒎𝒎𝒂𝒓𝒚, Server Components handle rendering and data securely on the server with minimal client JavaScript, while Client Components enable interactivity in the browser, making Next.js apps faster, safer, and easier to build. #react #nextjs #javascript #servercomponents #clientcomponents #frontend #interview #WebDevelopment #opentowork #readytowork #immediatejoiner

  • Server Components vs Client Components

To view or add a comment, sign in

Explore content categories