Learn how to seamlessly call and manage Next.js server actions from client components using the React 18 useTransition hook! I’ll walk you through why you need this pattern, explain each step with code, and show you how to handle form submissions, pass data, manage pending states, update UI, and debug common issues. Whether you’re building forms, dashboards, or any interactive app, this video will help you make your Next.js apps faster, smoother, and more responsive. Subscribe for more practical Next.js tutorials! ------------------------------------------------------------------------ 🚀 Boost Your Skills with these Pre-Requisite Videos:
⌛TIMELINE⏳ 0:00 Intro: What are server actions and why use useTransition? 0:50 Explaining useTransition hook and basic syntax 2:40 Two key steps you must always follow: define server action, call from client 4:05 Getting user data from a form with FormData 6:20 Calling the server action and passing the data from client to server 8:40 Updating client state after the server action completes 9:40 Running the app and checking for warnings/errors 10:50 Re-running and verifying the UX—watch for flickers, loading states 11:30 Challenge for viewers: Try implementing this pattern in your own project
Yes guys, this video is very important
because if you remember
this use transition hook is also very important
which was told in version 18
now if you see our course
then we have already seen
version 19 hook
that's your use action state, use form status
we have seen all that
now we will see version 18 hook
and that's your use transition
which was one of the most popular hook of version 18
you will get to know that in this video
that how you can handle
server actions with the help of use transition hook
so guys
as you can see
use transition hook returns an array
with two elements
first one is pending state
which means your transition is in pending state
or not
in simple language it is a loading animation part
from where you can show
till the time data is fetched
we will show loading animation to user
second one is
start transition
this is a function
whatever you pass in this function
we call it as action
and this is very useful in next
if we want to handle server action
we can call it easily
and one more thing
if you call start transition function
whatever you write in this
how react treats it
it says that
there is no urgent work
in background
whenever there is a need
we will call it
and also
our user interface
whatever work is going on
it will not disturb
so this is a very good hook
and you can see
they have explained very well
and there is a convention
you can see very clearly
you can see
in start transition
if you pass any function
we call it as action
convention says
whatever function you are passing
by using a callback
that function
suffix means end
prefix means before
you have to add this action word
like if you create a form
and let's say
when someone is submitting that form
and you have to call an action
so your name should be submit action
so this is your server action
in next world
and I hope you understood
button is disabled
in pending state
if there is a transition
now I will tell you what is it
and it is very important
and I hope you are enjoying this course
so let's go to our vscode editor
so you can see
we are on our vscode editor
and this is the code
basically this is the hook
which you get in version 19
and that's your use action state
and your first element
basically your first argument
is your server action
and your state
and here we also getting is pending
so this pending
I was getting there also
now see here
I have commented
now without this hook
how can I get the data
and how can I call
my server action function
it is very simple
and by the way
I am removing this
so that I can show you
in simplified way
so what is our first task
first task is
I want the data of
the form which user has filled
then I will pass that data
to my server action
and in server action
I will get that data
and I will insert it
in our database
basically I will insert it
so my task is
get the data
what user has written
and call server action
with all those parameters
this is my task
so first of all
how can I get the data
which user has written
so first of all
why I have written form action
because
I was getting it in use action state
but now
I am not using that
so that's why
I want to create a new function
handleContactSubmit
alright
I am going here
now here
see
version 19
in react
when you can use
these attributes
so here we get one more power
and that's your form data
so in this form data
we get all the data which user has typed
so guys what I will do
I will directly destructure
and basically
name attribute
in each input field
is very important
so that's why
I know I am going to get
full name
we have email and
message
now how I will get it
very easy
whatever I told you in form data
we have form data.get
.getAll.entries
form data.entries
if you remember
we can write like this
but guys
when you write form data.entries
what it does
it returns data in array of array
and I want to convert it
in exact object
so for that
object.formentries
if you write
by the way spelling was wrong
that's why
email
like this I got all the data
very easily
my first step is done
and that is
I got all the data
which user is typing
now one awesome thing is
if you write object.formentries
you don't need to write this
you simply wrap form data
in this and you will get
all the data in a snap
it is so easy
like after version 19
else you have to target
event.
but after version 19
remember
in version 19
now you got all the data
now you have to call
server action function
and here
you have to pass
form data
ok
now
finally
your useTransitionHook
will be useful
so we have a useTransitionHook
and we need to import it from
react
and
you don't need to pass any parameter
but yes
it return an array with two elements
like your useState
but totally different data
it is returning
I just told you
and here we have a start
transition
this is it
now my function
it has a power
that I can pass callback function
and
whatever function we pass
it is your action
and my contact
action
I will call it here
and no doubt
I have to pass data
so my data
I will pass it like this
ok
and I will call it a response
and by the way
whenever you deal with server action
we need to make it async await
you have to make it asynchronous
you can handle it through promise
by using async await
you can handle it like this
and here
you have all the data
basically
it is submitted successfully or not
it is inserted successfully or not
so that data is also here
now I have to show that data
successfully submitted or not
so what I will do
here we have a state
when you were using action state
you were getting a by default state
but here we don't have
so what we will do
here we will create our own state
let's go with
contact form
form response
now obviously
form
you have to write form not from
and here you have to use
state and put null value
for now we don't have any response
what we will do
set form response
I will pass my response
so at least
whatever it is
is it returning
for now we are not returning anything
it is redirecting
which is ok
but if it is returning then what
let's see the return
then guys
your contact form response
this is
here
here you have to pass like this
because earlier it was state
we don't have to worry about pending
because by default
with the help of use form status
I am able to get it
let's go here
it is already running
let's go to my project
I want to go to my contact form
you can see like this
so guys I will write here
use transition
now if I submit it
let's see
error while submitting
so guys now our contact action
come to server action
first of all we don't need previous state
right
we don't need it
second thing is
if you remember
now I am directly passing whole data
if you see here
here I have passed
whole data
so I can directly
destructure it
and I can remove it
so one second
here I have to do something
it will be here
instead of this I will do this
there is no need to destructure
I will directly comment these two
and by the way
I will directly insert this full name
you can see
how easy it is
let's check it out
let's refresh it
I will write
testing
now let's check it out
form data is not defined
so guys here
because of mistake
it was deleted
but I will add it
let's check it out
I am writing test again
let's see
form submitted successfully
means without using action state
how easily you can handle server action
and beauty is
here all power is in your hands
you don't have to worry
very easily
how to send your contact action
if you want guys
you can remove it
and simply pass form data
and you can do extra steps
but we kept it simple
and our work is done
so guys this is it
I just wanted to show you
how your use action state works
in the same way
in version 18
how you can perform
same action
so guys
it's totally up to you
you can do more deep research
what you can do
and what are the advantages
but yes
I hope you like this video
there are some things
which you know
so guys
if you have any doubt
please comment
I will reply to every comment
please like this video
and share it with your friends
and our next target is
till then guys thank you so much
take care