![]() |
Next.js, a popular React framework, helps to build powerful web applications with ease. One of its standout features is the ability to seamlessly integrate server-side logic alongside client-side components. This allows for dynamic content rendering, efficient data fetching, and enhanced user experiences. In this article, we’ll explore how to use server-side components within client-side components in Next.js applications. Prerequisites:Table of Content What are Server Components ?Server components were first introduced in Next.js 13 and are rendered on the server which reduces the amount of JavaScript shipped to the client computer, enhancing the performance of the website. All the components are by default server components and you have to specify ‘use client’ at the of the file to convert it to client rendered component. Server components are better for Search Engine Optimization and there is no initial white screen like normal react components. However, importing a server component inside a client component will be of no use because it will rendered on the client side. Steps to Initialize a Next.js projectStep 1: To initialize next.js project enter the below command in terminal. npx create-next-app@latest ![]() Project initialisation Step 2: To start the app type in terminal npm run dev Step 3: Create components folder and create 2 files ClientComp.tsx and ServerComp.tsx inside the folder. If you are using Linux , then you can do the same by running the command below. mkdir components && cd $_ && touch {ClientComp,ServerComp}.tsx Dependencies: "dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.0.4"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.0.4"
} Folder Structure:![]() Project structure Importing Server Component to Client ComponentExample: Now that we have initialised Next.js project, lets create simple client and server component.
Output: ![]() Server Component in Next JS Looks good, then what is the issue? ProblemServer component is not rendered in server side but is being rendered on client side. You can verify this by going to source tab in chrome developer tools. ![]() Server component is rendered in client side
ApproachTo use the server component inside a client component we will pass the server component as a prop to the client component. This approach will avoid the problem of displaying server code the developer tools and ensure confidentiality. SolutionThe page component is server side. So if we can import the server component and pass it as a child prop to client component. In this way server and client components are decoupled and it will solve our issue. Place the children where you want to data to be placed. Behind the scene, Next.js is rendering the server component before the rendering of client component.
![]() Server component is rendered in serve |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |