Are you confused between React Server Component and Client Component in Next.js? 🤯 Don’t worry! In this video, I’ll explain the exact difference between RSC and RCC using simple words, real folder structure, and live examples. We’ll see how to create each type, how data fetching works differently, and why you should care about async functions, API key visibility, and component composition rules.
⌛TIMELINE⏳ 0:00 Difference between RSC and RCC in Next.js (Live Example) 0:35 What is a Client Component? 1:20 What is a React Server Component? 2:05 Simple Definition: RSC vs RCC 4:50 Next.js App Router Folder Structure 7:10 Creating Client and Server Pages 7:45 How to Make a Client Component 11:08 ❌ Why "use client" Components Can’t Be Async 12:20 React.js Playlist 12:50 Fetch API in Client Component (useEffect) 16:20 How to Fetch Data in React Server Component 19:35 Data Visibility: Client vs Server 22:30 Important Rule: Composition Makes All RCC 27:10 All Differences Recap – Quick Summary
Hello, what's up guys? Today in this video, we will see React Server Component vs Client Component in our next application.
Yes guys, today's video is very important and I think all of you were eagerly waiting for it.
What is React Server Component and what is the role of Client Component?
Because wherever we work, a simple file comes, we write our code there and the work is done.
But guys, we all know that Next.js is a full-stack framework.
It means you can write both backend and frontend code here.
So guys, I'm going to clear all these doubts in this video.
First of all, let me tell you in a simple language that what is the difference between Client Component and Server Component.
Like whatever you used to do in React, whether it's with hooks, event handler, onclick and all those things.
So all those things that you used to do in React, now you can do the same in Next.js.
We all know that.
So to do all those things in Next.js, you have to make Client Component.
This is the difference.
Got it?
If you want to do all those things that you used to do in React in Next.js, then you have to make Client Component on the Next.js application.
Okay?
And don't worry, I'm going to explain it to you in a practical way.
That's why you will definitely see it till the end.
Now second, Server Component.
If you want to do some server-related code in Next.js application, then you can use Server Component.
Okay?
In Server Component, if you have some data in the database and you want to get it and display it here, then you will use that.
If you have some API keys and you don't want the world to see it, then you will keep it on Server Component.
And then guys, if you can see FCP here.
First Contentful Paint.
This means that if you are opening any page and you already get it ready there, rather than loading one thing first and then loading the other, then it takes time.
But on the spot, everything is loaded quickly.
So if you want that functionality, then you get it all in Server Component.
Okay?
Now guys, I think that by telling you so much, you have got an idea somewhere.
I will simplify it further for you guys.
So that whenever anyone asks you, what is the difference between React Server Component and Client Component, then you give a very basic overview.
Whatever front-end code you want to write for React, I will write it in Client Component.
Whatever back-end code I used to do through Node Express and MongoDB, I will write it in Server Component.
So you will write the front-end code in Client Component and the back-end code in Server Component.
Just keep one thing in mind.
The way I explained you Client and Server Component, you got to know.
But next time if someone asks you, your friend, then whatever I just said, front-end code in Client and back-end code in Server Component in Next.js application.
That's why it's called your full-stack framework.
Because you can do back-end and front-end everything in Next.js application.
Right?
So you can share the same thing with your friends.
But if someone asks you in an interview, then I request you to read a good definition of both of them.
Go there and say it.
I just told you this so that it fits in your mind that what are the differences between the two.
Okay, I'm doing it practically.
If someone asks you in an interview in a company, then I said don't say front-end code, back-end code like this.
Okay?
All right guys, now let's go straight to our VS Code Editor.
And finally, now we will do it one by one practically.
But before that.
How does the file of Server Component look like?
How does the file of Client Component look like?
Okay?
How will I know whether it is a file of Server Component or a file of Client Component?
Let me clear this doubt first.
Okay?
So listen carefully.
Whenever you install Next.js application, you will see a package like this.
Basically, you will see a file structure like this.
If you select App Router, then you will see an app folder.
Basically, this is used in our routing.
Okay?
And we read in the navigation that whenever you create a folder in the app folder and write page.js in it,
it means that now this becomes your about page simple.
If you are making a service folder, then it becomes a service page.
But in this condition, you will have to write page.jsx in it.
It becomes a contact page.
So, which React Server Component is there in this?
Or which Client Component is there?
How will I know?
It is very simple.
Whenever you see an app folder,
whatever page.jsx, layout.jsx, or whatever file you create in this folder,
by default, they are your React Server Components.
This is the most important point.
All the files in the app folder are by default your React Server Components.
Except one file, that is your error.js.
Okay?
You can check that out.
Now you will say that I want Client Component.
And it is very important.
Because if you want to use hooks,
let's say you want to make a simple counter application using a useState hook.
You can't do that here.
Let's say you have a button on which you want to add a click event.
Let's say onClick.
You can't do that here.
If you want all those functionalities,
then you have to convert these files into Client Components.
Okay?
How? I will tell you.
For example,
I said I will show you live.
Watch carefully.
This is our homepage.
I will add a button here.
I added a button here.
I will add ClickMe.
ClickMe.
It should be deep.
I am using a fragment here.
For now, I used a button.
I got a button.
I will quickly add a class name on this button.
That is busy.
I will make the text black.
Okay?
Something looks like this to me.
And I will give a little padding.
Let's say I gave padding of 5.
Okay?
I would like to click on this.
By default, all the files are called React Server Components.
Okay?
Basically, it is just a component.
It is just a React Server Component.
You can say that.
I will do one thing here.
I want to add an event.
And that is onClick.
Okay?
I will click here.
Simple.
Whenever someone clicks on this,
I am alerted.
Basically, I have to show a high.
Nothing else.
Simple.
Let's check it out.
This is a React Server Component.
Look at this.
Event handlers cannot be passed to client component props.
Look.
You will get an error like this.
But it is important for you to understand that
you cannot use this onClick in React Server Components.
And, I mean, you are understanding.
It is like a small thing.
I have to click on a button and show an alert.
You cannot do it directly.
You can pick up any file.
You cannot do it in Next.js or Prowler.
If you want this functionality,
you have to create a client component.
Now, I will tell you how your client component looks like.
I hope you are enjoying it.
Okay?
Stay with me.
At the end, all your doubts will be cleared.
And you don't have to go anywhere after this video.
What I do is,
I create a folder in Users.
I write,
Client Component.
I create a file in it.
Page.jsx
Okay?
I have created it.
When I am creating,
I also create a server component.
I also create a page.jsx in server component.
Okay?
Right now, I am on client.
And, I am going here.
Client Comp.
I give a name like this.
Async.
Now, remember one thing.
If you are using a client component,
you cannot make a component async.
Note this point down.
I know I said it suddenly.
But, note this point down.
In client component,
if you are making a component like this,
you cannot make it async.
You cannot make an async component.
And, you have to export.
Default here.
Client component.
Okay?
Now, I am going here.
I return here.
Okay?
And, the button here,
which I created a while ago.
This one.
I copy it.
And, I paste it here.
Okay?
So, this is inside my client component.
Okay?
And, I want to do one more thing.
I go to navigation quickly.
And, here,
after about,
I want to create two files.
That is your client component.
Okay?
You write the spelling correctly.
C-L-I-E-N-T
Okay?
And, I write client here.
Okay.
I keep client component here also.
And, I made one more.
I write server component here.
Okay?
Alright.
Now, I want to go here.
So, I am still getting an error.
Just because of this particular part.
So, I will comment it now.
Because, we cannot do that.
So, everything is fine.
This is my home page.
I go to my client component.
Okay?
Now, you can see that
on client component,
that page is not showing.
Client component also exists.
Let's check it out.
What happened here?
We are inside users.
Okay.
We have a client.
Oh, my God!
You didn't need to give capital to the file.
You,
client.com.
Okay?
Now, there is no tension.
I will go here.
Let me refresh it once.
Okay.
Again, I am getting the same error.
And, that is
you cannot pass onclick like this.
Now, this is because
I created a file inside the app folder.
And, the moment I created a file,
by default, what happened to you?
React server component was created.
I have to make it client component
so that I can use button, onclick and all those things.
Okay?
I can use hooks.
I can fetch data with the help of useEffect.
So, if you want to make React server component as client component,
then you need to write
useClient.
This is it.
You have to use this keyword.
And, this is it.
You must have seen this many times.
So, finally, in this video, your doubt is cleared.
If you write useClient at the top,
then this is now your client component.
Now, you can do all those things in client component.
And, boom, guys!
This is it.
Look at this.
Now, your high is here.
So, in client component,
you can do any part of React
here.
Right?
And, in server component,
you can write server-related code,
create API in backend,
directly, without async,
or fetch data without function,
get data from backend,
and all those things.
You can do all those things there.
Now, you know
what is the job of server component
and what is the job of client component.
How does server component look like?
And, by default, all are server components, by the way.
And, if you want client component,
how does it look like?
Wherever, in any file,
you can see useClient at the top,
that is your client component.
Right?
And, I added one more point.
Whenever you use client component,
you can't make your component async.
If I do this,
Right?
Look, I didn't get anything here.
But, at the bottom, you can see red.
What is it saying?
Client component is an async client component.
Only server component can be async at the moment.
Did you remember this point?
This error is often caused by
accidentally adding a useClient to a module
that was originally written for the server.
I mean, we know this.
Why didn't the page show this error?
What does it think?
Maybe, you were working on the server page
and you accidentally added useClient.
So, now, it is asking to remove it.
Why should I remove it?
I only need client component.
I will remove it, right?
So, note down this point.
You can't make async in client component.
But, yes,
if you go to server component,
look,
I didn't use any promises here.
But, I am using async by default.
Whether you use it or not, it doesn't matter.
But, you can use async in server component.
You can't use it in client component.
Now,
pay attention to one more important thing.
I will do one thing.
I will fetch a data and show it to you.
I told you,
this is a very important video.
It can be a little long.
But, in this video,
I will clear all your doubts.
Do comment and tell me
whether you liked it or not.
And, finally,
whether your doubt is cleared or not.
If you share your post on Instagram,
I will be very happy.
Because, many people have doubt in it.
Watch carefully.
Let's say,
I want to fetch a data.
Through API,
how we used to do it in React.
I am copying and pasting it directly.
Because, I know you can do it.
And, I have already uploaded
the world's best React playlist.
You can see it here.
I don't think you will get a better playlist than this.
I guess, you can see it in the playlist section.
I hope, you can see it here.
I will add it in editing.
You can see it there.
What is useEffect?
It is simple.
I have added a callback function
and dependency array in useEffect.
I want it to run for the first time.
If I want to fetch data through fetchData,
how do I do it?
I create an async function.
I create a simple async function.
I fetch it.
And, by the way,
you can see the URL.
Guys,
this is the URL
where I am calling the JSON Placeholder.
Okay.
Await.
I got the response.
Further, we convert it into
readable object format.
We get the data by using .JSON.
I am getting the console to do it.
And, I am getting the return to do it.
So, I have returned the data in this way.
Now, I have got the data.
I am getting the return data.
I can do one thing here.
And, that is,
whatever the data will be,
I will run it here.
Our component is over here.
So, guys,
what do I do now?
I create a useState.
postDataSet
postData
will be equal to
your useState.
Okay.
And, yeah, this is it.
And, here, I keep an empty array
for now.
Initial.
And, obviously,
if I have used useEffect,
then I have to show it here.
Perfect.
See,
I am doing this
so that this video is very important.
I am showing you useEffect
and I am showing you useState.
Now, what will happen here?
Whatever the data is,
okay, whatever the data was,
I am returning it.
It's a good thing.
But, at the same time,
I would like to do one thing.
Now, this data,
I am not keeping it directly above.
I am doing it directly here
in this way.
And, here, I will do one thing.
In useEffect,
if that data is ready,
then in setPostData,
whatever data I have,
I am passing it.
And, I am doing one thing.
Data.
Okay, fine.
I have passed it in this way.
Okay.
Now,
in this postData,
an array must have come.
Okay.
In postData,
my current data will be
that data which I am getting
through API.
Now, what I have to do is,
I have to show it here.
In this way,
what I did is,
I took a UL.
A simple grid.
In the grid,
I said,
give me a column.
Keep a little gap.
And, instead of data,
what I have to use?
Here, postData.
Okay.
I have mapped postData.
And, I said,
whatever is the body of the current element,
I have added an index.
Now, if I do this much.
And, by the way,
I don't need to write return here.
Because,
I am already writing return up there.
Right?
So, guys,
save the controller.
And, boom, guys.
See, you have got
the entire data of your API.
So, in today's video,
guys, you tell yourself.
If we were reading React,
we would have seen
this useState, useEffect
after such a long time.
But, see next,
how easily
I can write
all the code of React
in my client component.
Okay.
And, should I tell you one thing?
Now, if I
want to do it
through server component,
I can do it in a snap.
I will tell you
Okay.
For that,
we wait a little.
But,
here you got to know
how your client component
looks like
and how your server component
looks like.
Okay.
Now, at the end,
I am going to share
a very important point
with you.
I don't want you
to miss that.
So, I will tell you
that at the end.
Okay.
If you miss that,
then it will be difficult.
But,
I will tell you that at the end.
Now, I will do one thing.
See,
here,
to fetch API data,
in our client component,
we used to code like this
in React.
And,
here also,
we code like this.
It's not a big deal.
But,
if I want to fetch this data
in server component,
then,
by default,
we get async function.
So, see what happens.
I will go to
my server component.
I am removing
all these useless things.
This is my
client component.
This is my
server component.
Done.
In client component,
see carefully,
it was such a big code.
Isn't it?
I will do one thing
on server component.
I am going here.
I will write it
as server component.
Here,
I don't need
Obviously,
here I have to
default
app my server component.
I have to do this.
No doubt.
Okay.
Now, see carefully.
Here,
a beauty is that
now,
it is working
like a backend.
Okay.
Here,
you have to
call server.
You have to
call data
in server.
So,
what we were doing?
In client component,
in use effect,
we called
I want
it to work
only once.
In this way,
we called
a function.
Even,
I defined
then,
I wrote
this code in it.
Now,
to add this data,
I added it
in use state.
Do you understand?
How much
I had to do?
How much
I had to do
to show
KPI data
in my client component?
Do you know
in server component?
Nothing.
I am copying
this part.
I just copied
this much code.
I will paste it
directly in
Here,
I don't need
Okay.
I will do it
like this.
This is it.
By the way,
I don't need
to return it.
So, guys,
this is the power
of your server component.
Here,
you don't need
any function
in function.
Because,
by default,
this component
is your async component.
Here,
without async function,
you can call
directly in this way.
This is called
top-level
oversight.
But,
we can do like this.
And,
here,
this part,
written part,
I will copy it.
I will go here
and paste it.
Obviously,
I can't do this button
here.
So,
instead of client,
I will write
server component.
Okay.
And,
obviously,
data is already
in data.
So,
this is the beauty.
Means,
in server component
and client component.
In client component,
if you want to get
data through API,
we do like this.
In server component,
you can do it
I will go
in server component.
And,
it is telling something.
And,
that is,
we will refresh
Fill to pass
URL from
Oh,
Oh my God.
We have done
a mistake here.
It is not able to
get data through
Because,
where is the value
in URL?
Where is this value?
This is required.
Okay.
I am keeping this.
I am pasting here.
Now,
I will go here.
And,
boom!
Guys,
see this.
So,
guys,
how did you like it?
It was awesome.
Now,
one more thing
you have to remember.
Remember,
I told you
I will tell you one thing
There is still time.
But,
one more important thing
you have to remember.
In this URL,
you have written
data like this.
Right?
And,
this data
you are writing
like this
in URL.
Let's say,
tomorrow,
if you have
an important data
or an important
password.
So,
let me tell you one thing.
If you are in
server component,
if I inspect,
if I go to
I am refreshing
one more time.
So,
somewhere,
see,
if you see here,
I don't have
any idea
that
my API,
which API
through which
I am getting data.
I can't see it here.
Means,
whatever,
I mean,
in server component,
whatever you create,
constant URL,
variables,
or your API key,
you can't see it.
I mean,
client can't see it.
We can't see it
here.
it is said.
See carefully.
Use API keys,
tokens,
and other secrets
without exposing
them to the client.
It means
this.
Means,
here,
I have written
URL.
I can't see
this URL here.
I mean,
I don't know
that
from where
I am getting
data.
Main
apply
app.internal.js
No.
Layout.
There is nothing
in inject.
There is nothing
here.
Why not?
Because,
whatever data
I am getting,
first,
in server.
Then,
I am getting it.
See carefully.
If I click
on client component,
and boom guys.
See,
I got post.
I will click
on it.
And see,
response is coming.
I will go
on headers.
And this is it.
See.
What is this?
Means,
if you
write
any
secret
data
or
secret
API key,
you can
get it easily.
Here,
you can see,
in client component,
I am getting
everything.
My API key is
getting exposed.
My secret
data is getting
I am also
getting to know
from where
I am getting
data.
And,
what is
there,
what is not,
I am getting
everything from
But,
if I go
to server component,
one second,
here is the
server component.
preview,
response.
See,
on headers,
at least,
it is telling me
that this is
the URL.
But,
tell me one thing,
is this really
my URL?
A while ago,
in client component,
I could see
my JSON placeholder,
my actual
API.
But,
here,
I can't see it.
So,
I hope,
this doubt is
cleared.
Use API key,
tokens,
and all those
things.
And,
it says,
fetch data
from database
or API
close to the
source.
I have shown
this and
this.
This state,
event handlers,
I have also
told you.
On click and
on change.
And,
even,
this use
effect.
Now,
the most important
thing is,
finally,
that time has
come.
I hope,
you have
understood
React code
is in use
client.
Rest,
server-related
backend code
is in
server component.
And,
this async
can be used
and not
in other place.
You know
this.
Now,
the most
important thing
is,
by default,
app folder,
error.jsx,
will be
in server
component.
And,
we have
seen that.
Now,
there is
a condition.
Listen carefully.
When you
make client
component like
this,
and,
inside
client component,
if you
create
component,
let's say,
I am
create a
component.
Let's say,
I want a
simple
counter.
I want
a counter
app.
I am
creating
client
component
using
counter.jsx.
Listen carefully.
Fat
error function.
Now,
there is
a beauty
here.
You
don't
need to
export
default.
Because,
this is
your
component.
Now,
import
it.
Now,
I need
a counter.
Counter is
simple
add.
Let's
do
const.
Let's
write
set
increment.
Let's
write
use
state
and
by default
is
zero.
Now,
return
type
button.
I am
and
I am
showing
the
data
that
is
inc.
Let's
click
here
ok.
So,
I am
creating
set
increment.
I have created a simple counter in which I will get my previous data
So, I will add my previous data to it
I have created a simple counter in which I will get my previous data
Now, I have saved it here
If I go here
This is my client component
Now, I can see add here
It's a good thing
I will paste a good class name to it
So, it will look nice
And instead of Amber, I will go with Red
Alright!
Watch carefully
If I click, what is happening?
I mean, have you noticed or not?
What is this?
It was a simple counter application
But, it is working
See, it is working
Increment is happening here
But, have you noticed one thing?
Here, I have not written useClient
Where have I written useClient?
I have not written it
And if you have not written useClient
What does it mean?
It is your server component
And if you are working in server component
You can neither use hooks
Nor you can use onClick event handlers
Basically, you can't use any of these
But, I am able to do it
Without useClient
I have used hooks
I have used onClick
And I have created a React app
I have created it
So, how did this happen?
How is this even possible?
The rule is
Once you create a useClient
Any file
Any page
If you have created a client component
In that file
Or in that page
Whatever component you use
By default, it becomes your client component
Remember this
Do you understand?
In the client component page
Whatever component you add
By default, it becomes your client component
And this is the reason
Without writing useClient
I am able to use onClick
And I am able to use useState
Which is our hook
So, this was the most important
And the interviewers can ask you
Show me this
How is this working?
You can answer
Sir, maybe
You didn't show me the parent component
And the component you are calling
Maybe it is a useClient
Maybe it is a client component
Don't use the word maybe
Say directly
Sir, you must have created a client component
In that, you are calling it
That's why, without using useClient
Your program is working
Otherwise, it is not possible
The way you are confident
So, yeah guys, this is it
For today's video, let me tell you
I guess, you will get to know from here
If you want a client component
Then you have to write useClient
Else, by default, it becomes your server component
In the client component
You can understand
Whatever code you use in React
Basically, hooks, onClicks
Lifecycle, custom
Whatever client related work
You can do it here
If it is backend related
Or server related
Like fetching data from database
Or I have shown you
Fetch data, we can do it in both ways
But there is an advantage
If you want to keep the data secure
If you don't want to expose
Then also, you can use server component
If you want your first content
Full paint, if you ever
If you do SEO
When you load any page for the first time
How fast the render is
It depends on it
So, that functionality
It is good for SEO
Because the FCP score is good
In the next application
Because it fetches all the data
It just dumps it and shows you
But if you talk about React
Half of the data
You get on the browser
Then it requests on the browser
Then it gets the data from there
So, it takes time
Apart from this, I have told you
Async
You can't make client component async
You can't make it async
Server component is there by default
There is no tension
And then third
If you have any important data
Like I have shown you
Like this URL
It gets exposed
But you can't see it in server component
And the last
The most important
If you ever make a client component
If you ever make a component
Then by default
It becomes your client component
And that is the reason why
We can easily state
Without using useClient
We can use it
I think you should not have any doubt
If you want to write frontend code
If you want to write React code
If you want to write useClient, Node, Express
If you want to write database code
Now, understand it
Don't say this to interviewer
It is in your mind
React server component
Client component
It is in your mind
Now, we will make project
You will get idea
But you know this much
Because I have told you live
I hope you like this video
You can read more
But guys
As much as I said
If you understand it
It's enough
You will understand if you practice
Ok I had to write it here
It will come when it comes
So, guys I will be happy
If you will like this video
Share it to your friends
Share it on Instagram
So you can watch this video
Explained it in such an awesome way
I am very happy that I will post it on my story
And I would like to tell you
If you have any doubt, do not worry
You know that this time
I tried to make this series a little interactive
So that you do not feel like
I put a video on YouTube
Now I have an error, what should I do?
I am solving that problem
How is it that I read your comment
I am also replying to it
Sometimes I make a video on it
What else do you want?
And by default in Hindi
I explain in such a childish way
So guys, it takes a lot of effort
Source code and notes are also free
I am sharing with you
I am doing everything
Even to make such a video
Think how much time it will take
I have to sit with a few team members
So all this is not happening for free
So I don't need much
Just subscribe to my channel
And share it with friends
So guys, please
Subscribe to my channel
See you in the next video
Thank you so much
Take care