In this video, we’ll understand Server Actions in Next.js with a real registration form example from our Job Portal App. You’ll learn how to use the use server directive, handle form submission, and extract user data using Object.fromEntries() — all without saving it in a database. Perfect for those who want to master Next.js Full Stack Development step by step!
------------------------------------------------------------------------ 🚀 Boost Your Skills with these Pre-Requisite Videos:
⌛TIMELINE⏳ 0:00 — Intro to Server Action - Registration Form 1:40 — What is Server Action in Next.js (Simple Explanation) 3:10 — Using use server Directive in Next.js 4:25 — Importance of name Attribute in Form Inputs 6:00 — Creating Server Action Function for Form Submission 8:45 — Using Object.fromEntries() to Get User Data 9:40 — Have Doubts? Ask in the Comments!
Hello, what's up guys. Welcome to the nextgest project job portal application.
And today is our day number 13, where we have to see how with the help of server action, we can get the data of registration form.
Not only registration form, login form or any form data, you can get it very easily in nextgest.
I would like to tell you one thing, I have already uploaded a video on server action.
So, if you haven't checked out yet, please check it out.
Rest, I will tell you how you can get our full stack application like with MySQL and Result ORM.
Today, I will tell you how we can get the data of our user in a snap.
And by the way, today you are not able to see my face.
Because there is a little health issue.
But yesterday there was already a gap, I didn't want it.
If there is a gap today, I'm really sorry.
But as soon as I get well, I will try to bring a face.
But for now, the video is important for us.
So, enjoy the video.
And the video is very simple and sweet.
Where you have to get whatever the user writes.
How did we do it before?
First, if we simply submit any form.
You know, like we have a on submit.
Do you remember?
Like we used to have an event like this.
If anyone submits, we make a function.
In the function, we take a lot of data.
Or we mostly pass the backend and then store it in the database.
There were a lot of things, right?
So, what will we do here?
Our Next.js is a full-stack framework.
So, we don't need NodeXpress here.
So, guys, your job comes there.
Server action.
So, guys, let's quickly understand what server action is.
So, first of all, guys, this is a special function.
Which provides you React.js.
But yes, you can also use it in Next.js.
Because both are the same.
And it only runs on your server.
Not on your browser.
Okay?
And secondly, if you want to perform any operation tomorrow.
Because Next.js is a full-stack framework in itself.
Means, where we used to use React as a frontend, Node and then Express as a backend.
Here, your Next.js is a frontend.
The backend is also the same.
So, how do I call the API in the app?
Or how do I pass the data to the backend?
There is no such system.
So, you understand this.
Server action is such a part.
Where you have to pass data.
So, if you have any operation tomorrow.
Like querying in the database.
Means, insert, update, delete, whatever.
Then you have to call the API.
Or submit the form.
Or you have to do data mutation.
Directly from where?
Like your React components.
So, your server action is used there.
Without...
I mean, you see.
There is no need to create a separate API route.
If you have ever worked on Node Express.
Then you know.
There we had to create an API route.
But here you don't have to do anything.
Your client component.
See this.
This is your client component.
No doubt.
From client component, I mean.
You can call it React component.
Okay.
Now, directly from here.
You have to submit the form.
So, we can do that very easily.
With the help of server action.
And if you want to make any function a server action.
Or if you want to show any file as a server action.
Then for that, we need to use.
This use server.
Your directive.
Okay.
As I tell you.
You have to write it at the top.
And by the way.
These are all comments.
So, now I remove them.
So that you know.
Like this all are comments.
And this is your actual code.
So, now I tell you one thing.
Like.
You write this use client.
So that to tell you.
That in your next application.
This is the.
What?
Client component.
By default in Next.js.
Whatever file you create inside the browser.
They are what?
Server component.
But now what do we need?
Server action.
Okay.
Not component.
But action.
So, for that.
What do we have to write?
Use server.
This is it.
All right.
Okay.
So, now.
I need data.
Whatever the user is filling on it.
I need that data.
Okay.
It is very easy.
Okay.
I am here.
Once this server action.
I copy it.
And just remember one thing.
As we used to do before.
I told you in the starting.
Of the video.
Like on submit.
We used to write.
Isn't it?
Then from there.
Whatever data.
We used to forward it to the backend.
There.
We used to validate it.
And store it in the database.
Now here.
Nothing to do.
Submit.
I have a very comfortable way.
I have a nice role here.
And I'll go down a little more.
You'll just get this name attribute.
Once this happens.
There is nothing to do.
We have this.
Action attribute.
Where I have registered.
And I have already shown you.
Like this is our registration page.
Where currently I have.
Look at this.
Registration.
Action.
T. S.
X.
Its mandatory to Action.
T S.
X.
But yes.
From this.
I will know.
That I am doing this.
With the server action.
All right.
And I have imported this already.
By the way, here
see, like this
I haven't done anything yet, but I am going to
so, it's a const
we have this particular, ok
we have our registration
then simple, you have to make a server action
like this, ok
here, obviously, it will be asynchronous
because I have to do database query and all those things
now, the beauty is
here, your next.js
passes an object to your server action
and that's your form data
ok, that form data
passes to you, in which all your
values are
available to you, like
it provides some methods, like get,
set, append, and then
entries, ok, and form data
is also a built-in web API
like we have a request, response
and url search params
now, what is the beauty of form data, I will tell you
whenever you
console.log form data
we have a form data over here
so, guys, let's do one thing
console.log, and by the way, now you are using
type script, so it is telling
implicitly, its type is any
give some good type
we will give its type, whatever it is
form, and then we have this data
now, why I have given form data
because I have told you, here,
what is written
when you submit a form in the next.js
ok, so
using this type
the framework sends a form data
object to that server function
means, it is done
you just have to write this, and see, the error is gone
simple, alright, ok
I will save the controller, I will go here
and, ah, yes,
why, because I have to write export here
and that's the reason why
I have imported it like this
ok, now there is no warning
all that should not happen
so, guys, you can see that I have
filled the form here, I click on create
so, nothing happened, but yes
I will go here
and at the bottom, and boom, guys, see
form data
and see, my data is here
see, how easily my data is here
but yes, I didn't want it on console
I want actual data
so, now, what I will do
like console.log
if you want, form data
dot
get method, and here
like you want name, I have told you
name attribute is very important
because you pass the value
of the name attribute here
to get the actual value of that input field
right, but here
if it becomes 100, then
I will have to write it 100 times
right, so, I don't want to do that
if you want to see the get method, then watch this video
I will tell you directly, how professionals
will do it, simple, what we will do
form data dot
entries, ok, this will do
what I want to tell you
like you will get an array, what should be inside the array
like, value pair
like name, value
so, here, what will happen, let me do one thing
let me show you, what you will get
ok, now, let me do one thing
form data, I will keep it here
and let's check it out, what we will get
I will hit it again
ok, object, form data
iterator, you can see something like this
by the way, ok
so, here, you are not getting anything
so, now, let me show you
where is our code over here
so, for now, let me tell you
here, you are getting a nested array
inside the array, your element
is also in the form of an array
so, I have to convert it into a plain object
so, what we will do
I will
further, there is a method
which is provided by the object itself
if you are stuck in this kind of problem
do one thing, we have a form entries
ok, if we do this
let's check it out, what will happen
let me refresh it
let me check it
oh my god
see, I got such an awesome data
in the object, so easily
my work is done
my data is also in the object
now, I can easily destructure it
even I am getting xnid here
so, now, I will do
destructuring
destructuring is a kid's work
see, this is it
now, what I will do, this particular code
you can see
this particular code, copy it
paste it here
now, whatever you will write
in console.log
you will get the value of all these
now, you don't have to do anything
now, you have to store this data in a simple database
in a snap of fingers
which we will see in tomorrow's video
that's all for today's video
I hope you liked the video
and see, how easily we got the data
with the help of what?
server action
so, yeah guys, this is it
that's all for today's video
I hope you liked the video
and I know, we have submitted the form
we will see that in tomorrow's video
but, for today's video, I just wanted to show you server action
one more time, how easily
we can get the logic
in the next JS
so, if anyone has any doubt
please check out the previous video
especially, the server action
and guys, that's all for today's video
if anyone has any doubt, do comment
and if you liked the video
like, share with your friends
our next target is 8 lakh subscribers
please do it, see you in the next video
till then guys, thank you so much, take care