In this video.
Let's see how we can handle four in react four for handling in React.
We have couple of ways of doing it.
You can use a third party package like for me to do that one or we can do it by ourself.
In this case, we are going to handle the form by ourself without using any third party package.
So let's see.
So inside the log in component, this where we left.
Let's bring in what is called the You States, because we need to manage in ten states.
So let's get started.
Here we go.
You state by default.
This one is empty.
That is the email field going to be as that and equal to that.
And you have access to the email value here.
The function to update course set email.
Great.
And the next one going to be the password when I be asked password and then the set password.
But for this implementation, assuming that we have ten fields, let's look at the transaction in case
we are trying to add transaction.
You can see we have name type amount category, color date notes.
It means that we are going to create instances of you state almost five times or seven times and you
will see that our component will be missing.
So to avoid that, I'm going to show you the different way of handling form.
So here you go.
Let's remove all this one from here.
Let's bring in the you states.
But for this one, we are going to pass in the fields that we want to manage.
So the first one is going to be the email by default is empty.
And then the password also by default is also empty.
Great.
So what comes back is the actual state and then the function to update the state.
So here I'll call this one, ask for data.
And then what is called the sets form data.
The next step is that I can go ahead and then structure the phone data that represents this one.
So here we go.
Const object is equal to the for data and I want the email and then the password.
Great.
So let's have a comment and call this one.
As for data, the next step is that on each of the inputs, we are going to add an attribute called
name and this one is called email.
The next one is the password, so let's provide the password as a name.
It must be the same as we see inside the initial state here.
So next step is the own change.
And how are you going to handle that one?
Let me show you here, costs.
I'll make this one as unchanged.
Input and for this is equal to normal function as a function.
And I have access to the E inside here and let's try and console that log says that on change triggered
the next one going to be the submit.
So this one going to be submit handler and it goes like this on submit handler and it's equal to normal
function.
And here I will have access to the event.
And I have access to the event.
So here too, I can continue the blog and say that submitted.
So next is let's bind to the form.
Let's remove the action and provide on submit.
And the function is called on submit.
Until.
Great.
So let's locate the button for sine app and this is a button to click.
So let's change this one to button and let's remove the h ref.
But instead let's provide a type of subnet.
So now we can try it out.
So let's open the console and let's play this one.
And now let's provide some dummy email.
Password is also dummy password and let's hit send.
And you can see that we saw some message, but all of a sudden it vanishes because we are refreshing
our application upon submitting.
So to avoid that, we are going to use the event type to prevent that.
So here you go.
E dot prevents default.
And now let's check it out again.
Let me clear my console here.
Let me add anything here and the password in it.
Then hit send.
And now you can see that we have submitted.
The next one is going to be the change.
Let's by the on change to the individual input.
So for email, let's add a value called email.
Remember, we have the structure, the form data here for this one.
So the next one going to be the on change and going to be as on change input.
So I can copy this one.
And now down here that is a password PC.
And now the value two is going to be as password.
Now let's check it out.
Let's play the console when I tap anything.
You can see that it's being triggered.
Like what?
The password is also triggered.
Now we have two way binding.
The next step is that I need to dispatch the action or my function to log in.
So now moment of truth.
But to be able to achieve that, we need to update our states.
So how are we going to do it?
And we are going to do this one in a strange way.
But I explain to you to make it very simple.
So here we go.
We take the set from data, and then as a function call, we are going to pass it an object.
So here we are going to spread all the initial form data.
The next argument is how we are going to add the individual field and I'm going to use this syntax that
is the bracket.
And next is we pass in the e dot target.
Dog's name.
So what does it mean?
This simply means that we are taking the input name as a property.
And remember, for each of the inputs we have the name, email, and then the password, which is equal
to the initial state value here or properties here.
So if you try to console that log E to target name, we are going to see the names.
So let me do that one before I continue.
And let me paste that and let me comment this one out first and now let's check it out.
So here, let's open the console.
Well, and let's refresh let me clear it.
And now when I type in here, you can see that I have the email for the password.
I also have the password, so I'm going to use this one as a property.
So here we go.
I can now bring it back.
And that and the value is going to be as this.
Cullen and e dot target dot value for this one.
We know it already.
So let's remove this one and now let's try again.
I can see I cannot type anything here and it's working fine.
So let me close this one.
And now let's go ahead and call our function and pass in the form data.
So inside this, submit this where I want to call the action.
So here are going to be as dispatch action.
So here I'm going to call log in user action now pass in the entire form data.
So now moment of truth, are you ready?
So we are going to have a feedback inside the of context and which is this one remember we are console.log
again the success, likewise the error.
But for this we are going to face some beautiful error.
So let's see and then I'll explain to you what it means.
So now here we go.
Let's provide an account that exists that is the atom and gmail.com.
So copy that one.
And now let me pass it here.
And the password is one, two, three, four, five, one, two, three, four, five.
And now let's open our console here.
Let's click it before I hit send.
And you can see that we got what is called pending, meaning that we are not handling the problem as
well.
So here I need to put a wait in front of it.
So I store.
We are going to face some beautiful error that I'm trying to refer to.
Okay.
So here, let me add the email here and then any password here.
And now let me hit send and you can see that we got beautiful error.
So let's go open it and read more about this error.
This kind of error is not caused by us, but at the same time you don't know what kind of error it is.
So to know more about this error, let's play this one and now click on network and now let's click
everything by clicking on this one and let's try again.
All right, so let me click on the sign in.
And now here we have the name that is the path called Let's Look In.
And now instead stutters.
We have this error called course and this is what I'm trying to refer to.
So what is caused?
Well, before I explain, let's click on the log in and here we have headers.
Let's click on that.
And this is a URL that we are requesting.
And if you go to payload, this is the data that we are trying to send to the server and there is no
preview.
Likewise response.
So back to the course error.
When we say cross error, it simply means that if we are trying to access a resource from a certain
server into a different server, in our case from our backend server into React server, and this security
is really important because we shouldn't allow any server to access our API unless we tell our server
that hey server just receive or send data to this server who is trying to access the resources.
Because of that, we are going to configure our express server a little bit to be able to trust this
server, and that is our React application because our application is running on Port 3000 and our server
is running on Port 8080.
So it means that there are different servers altogether.