Redux Tutorial #2: Create Reducer Functions to Add & Delete Tasks in React

Thapa Technical
63 followers
2 Video Views·Oct 15, 2024
Hello, what's up guys? Today we are going to see Redux Reducer Function in a React application.
So, guys, if you haven't seen yesterday's video about what is Redux, then I highly recommend you to check out this awesome video.
And do check out our React version 19 complete series.
But now let's go straight to our second video that is about Reducer Function.
First of all, if you have worked with the useReducer hook in React, then you will be able to relate very easily and you will have a lot of fun.
So, here you can see very clearly that Reducer is a function that decides how the state should change based on the action.
Who decides how the state should change based on the action? Your Reducer Function decides.
The Reducer takes the current state and an action and returns a new state.
Don't worry, I'm going to tell you the whole syntax, then theory, then practical, everything in this video.
So, please check it out.
So, don't worry, watch the video very easily.
And if you don't have much interest in theory, you want to see the practical directly, then the chapters or timeline is already added.
You can skip this part and watch the practical part.
Now, two things that you don't want to forget in life whenever you work in Redux.
First of all, this Reducer Function, it must always return a new state.
And the second, the most important thing, this should never modify the old state directly.
You can't directly modify or mutate the old state.
That's why we always use the spread operator to duplicate the data.
So, if you remember these two things, it will be very easy for you to work in Redux.
Never change the old state directly.
Second, you always have to return the new state.
Let's understand a little more about the syntax of the Reducer Function.
So, here you can see very clearly, if I show you.
So, if I show you here, you can see that what you see is your syntax.
Reduce the function.
And then, what was said, it takes two arguments.
One is your state.
And the second one is your action.
If I show you the definition of this, you can see the state here.
This is what your current state.
And then, guys, we have this action, which tells the Reducer what to do.
It has a type and sometimes a payload.
Again, look carefully what is written here.
It has a type and sometimes a payload, which is the data.
This is very important.
Inside the action, we have something called type and then a payload.
Let me tell you what it is.
And, boom, guys, see, I told you, it will be fun.
So, here you can see that what was said in this line,
that the action has a type and then a payload.
So, this is it.
This is your action.
Near this action, we have something called .type method,
which finds out which action I have to perform.
And how will it find out? With the help of switch case.
Simple. We have a multiple case.
And then, along with that, action.payload.
Look here.
This is your type and then this is your payload.
Payload is some extra information, which we are sharing,
to perform that operation.
For example, if I want to increment,
then what I will send in the payload is plus 5.
I mean, I want to increment from 5.
I am telling you, extra information.
Not from 1, but from plus 5.
So, here, in action.payload, there can be a numeric 5 value.
For example, we are creating a to-do application.
So, what did the user type in our action.payload?
What did the user type there?
Buy mango. Buy apple.
Something like this.
And here, I have written about our switch statement.
You can see here, very clearly.
Use a switch statement to check the action type.
Based on the action type, the reducer updates the state.
Obviously, based on the action type,
our reducer function will update the state.
Basically, it will return the new state.
And it will never directly mutate or update the old state.
You have to keep this in mind.
And there are some best practices.
I mean, when you work in the reducer function,
you have to follow it.
For example, as you can see here,
very clearly, this thing is visible.
Earlier, we used to write normally.
There is no problem in this.
But, nowadays, it is said, even when you see DevTools,
there is a trick there.
That is to use the state domain and the event.
This is the domain.
You can call it a state.
Whichever project you work with,
what is its main state?
For example, our task.
We have to create a to-do application.
Here, you can add a task.
And if you come directly, write to-do.
But, after forward slash, what do you have to do?
What do you have to do in the to-do application?
Do you have to add? Do you have to delete?
So, which event do you have to do?
So, like this, guys, the name of your action,
basically, action type.
This is your action.
Which action do you have to perform?
Guys, my task is to add.
Guys, my task is to delete.
So, here, you have to write like this.
Here, you can see, action types.
I think, I use a combination of state domain, like task,
and event, like add, separated by a slash.
For example, task-add.
Then, immutable state.
Never directly change the state.
Always return a new state object.
Guys, understand one thing.
Here, you can see this particular part.
If you want, you can keep it.
I say this only.
If you feel so.
But, this is very simple.
We can never directly update our old state.
That's why we are spreading our data into a new object.
Like this.
And then, inside it, any data property,
I am just updating it.
And everything else will remain as it is.
So, guys, this was all about our reducer.
Now, I will tell you practically.
So, guys, everything will be set here.
So, let's go over here.
So, guys, you can see that I have come to my VSCode editor.
And I have already created a folder,
whose name is react-redux-youtube.
So that I can know.
So, now, what we have to do, guys?
Obviously, we have to work with Redux.
But, ultimately, what is Redux?
It is a third-party application.
It is a library.
Which I want to use with React.
So, no matter what,
first of all, I have to install React.
So, before installing React,
we need to use the Vite.
We will install React through Vite.
So, npm create.
Okay, write it like this.
Create Vite at the rate latest.
Okay, do this.
And then, it will ask you the name of the project.
So, you write.
Redux-youtube-thapa.
If you want, you can also write this
so that you know.
Then, select React, select JavaScript.
This is it.
So, here comes cd.
And then, we have to go to this.
Install npm.
Okay.
And then, simply, you have to write npm run and leave it.
So, today, in this video,
how to install Redux,
I will not tell you.
Because it is very easy.
And I don't think that
to create a reducer function today,
we actually need Redux.
Right.
So, what I do is,
npm run and then dev.
Okay.
Mine will run on 5.1.7.4
because something is already running on 5.1.7.3.
And I will tell you what it is.
So, guys, throughout this application,
this to-do application,
we are going to work on this.
Where you can write,
buy grapes,
apple, guava,
I guess.
So, if I hit enter,
buy guava comes.
I don't want grapes.
So, I deleted it.
So, all this is happening
purely with the help of Redux.
This beautiful to-do application,
I will teach you how to make it.
See, the project is simple.
And I intentionally keep it simple
so that I can explain everything to you in detail.
Because if it is complex,
then you won't understand.
Then it's done.
It's done.
So, this is very important.
Once you understand,
then you add the edit functionality.
You add the update functionality.
Right.
Which I have already told you through React.
Right.
Alright.
Okay.
So, this is running.
Here are all the applications.
So, first of all,
let's go to source,
app.jsx.
Hit this.
So, here we do one thing.
Here,
first of all,
let's zoom in const.
For you guys.
Hello Redux.
Redux is called Hello.
Right.
All together.
So, yeah.
Now, I have to export default as well.
Export default.
And then we have this app.
This is it.
Okay.
This is done.
So, now if I go here.
So, it's a 5174.
So, I should get that Hello Redux.
Perfect.
Okay.
I got it.
Alright.
Let it be.
Look carefully.
This is my source.
I am clicking here.
And you can see here.
We have this new file over here.
I will click on this.
After clicking,
I am in the source.
Not.
Yeah.
So, what I do is,
store.jsx.
I am typing this.
Okay.
Alright.
So, now,
I have a store.jsx file.
Okay.
In the source.
What I do is,
I am in the store.jsx.
Now, I have to create my reducer function.
So, how do we create it?
Look.
You have to write const.
Your reducer's name.
I am writing task reducer.
Because we are going to create two applications.
Reducer.
Okay.
And something like this.
Now,
I said that your task reducer takes two arguments.
That is your state.
And then one is your action.
Now, okay.
Now, this state is your initial state.
Okay.
So, let's do one thing.
Let's create our initial state.
So, const.
Initial state.
And let's make it an object.
Because in the future,
we can add a lot of things here.
IsLoading, IsError,
Filtering,
GridView,
ThisView,
ThatView,
And so on.
You know.
So, a lot of things can come here.
That's why I am making a task array here.
And,
By the way.
And remember one more thing.
Guys,
Never use assignment operator like this.
Whenever you are using an object.
So, it's your key value pair.
So, you have to give a colon.
Okay.
This is it.
Now, I can see a lot of things.
Because,
First of all,
My current state
will be my initial state.
Okay.
At least,
The problem is solved.
Now,
I will go inside this.
Okay.
Inside this.
First of all,
We need a switch statement.
Okay.
So, I put switch statement directly.
And guys,
If you don't know
what is switch statement
or how to use switch statement.
Then, I highly recommend.
Please guys.
I have worked hard
to make this world best
javascript course.
You won't get any better
than this.
12 hours,
5 hours.
So, this is part 1.
Basic javascript complete course
of 12 hours.
In this,
You can see switch statement,
for loop,
for each.
You will enjoy it.
Okay.
Alright.
So, this is the key.
Here,
You have to pass
your actual case.
Now,
How will we know
which particular case
user is trying to find out.
So,
This action,
Here,
action
dot
We have this type.
Here,
You can't see it.
But,
We have this type property.
In which,
whenever we dispatch
any action.
Then,
It will be written there.
That,
I have to perform
this particular action.
Like,
If someone clicks on plus.
Like,
If someone,
For example,
If someone clicked on
delete button.
So,
We will dispatch
an action on delete.
That,
My type,
Type of action
will be delete.
Like this.
So,
We will get it here.
If I want to add,
Like,
By,
Anything.
Right?
So,
If I click on this,
I want to perform an action
to add the data.
So,
Like this.
Okay.
So,
Action dot type.
Now,
Here,
Which actions can be there?
So,
Name of actions.
Okay.
I will go to top.
Over here.
I will write name here.
Let's say const.
I will write like this.
Add.
Underscore.
Task.
And,
I told you.
State will be task.
Or you can write to do.
And,
Event will be add.
Okay.
Like this.
So,
Copy this.
And,
Paste it here.
So,
Your new case is ready.
First case.
Has anyone dispatched
add task?
Obviously,
Whenever we click on this,
We will like to fire
this particular case.
So,
I am removing this.
Add task.
Now,
How will we add any data?
So,
Guys,
Don't think too much.
It's super easy.
Return.
Calibras.
Okay.
Now,
Look carefully.
This is our state.
And,
For now,
Value of this state is
this state.
One thing,
We have to always make sure
in your Redux application.
And,
That is,
Never,
You can mutate
your old state.
Can't update directly.
That's why,
We will make a copy of it.
Yeah.
This is it.
So,
I have spread
my old state.
And,
on a new object.
Okay.
You can call it a new property.
So,
Now,
I don't have any tension.
Basically,
I have made a copy
of our state.
Now,
In that object,
What do I have to update?
I have to update the task.
So,
Basically,
Task.
Okay.
Will be equal to.
Look carefully.
This is your array.
So,
My value will be
array only.
Now,
There can be a lot of data
in it.
Previous data.
Like,
In the beginning,
There are already four.
So,
I don't want to
add a new data.
No.
Previous data
should be as it is.
Comma.
And,
Whatever I am adding,
should be added at the end.
So,
Adding at the end is
a child's work.
Action.
Payload.
Okay.
We get data like this.
Payload.
Basically,
I will intentionally write
Payload there.
Don't worry.
We will see actions later.
But,
Now,
You understand.
Actions.
Payload.
It means,
What the user typed here.
I have added that data.
But,
Now,
How can I make sure
that my previous data
should be as it is.
It is simple.
Like,
When you wanted
your old state
to be as it is.
For that,
What did you do?
You spread that state
into a new object.
So,
Here also,
we have to do the same.
In state,
we have a task.
So,
In state,
we have a task.
What do we have to do?
Dot. Dot. Dot.
Done.
My work is done.
You understand.
So,
This is how your work works.
First,
with the help of this,
I made a duplicate object
of this.
And then,
I went inside the task.
In that also,
I kept my previous data
as it is.
I spread it.
I mean,
I kept its duplicate here.
And then,
whenever someone will add,
it will be added at the end.
So,
This is important.
You should never forget this.
You have to do this every time.
Okay?
Okay.
Your case is ready.
In the same way,
Let's imagine,
we need to make one more case.
And that is nothing but
this delete case.
Whenever someone will click
on this delete button,
then,
which case I have to fire?
Which action I have to perform?
And that time,
my action name will be what?
I will copy this.
I will make one here.
And that is like
delete.
Delete task.
And instead of add,
I will do delete here.
So,
I will add this delete task
in this way.
Now,
if I have to delete any data,
then,
how can I delete it?
Think,
how can I do it?
Right?
So,
this question is important
to understand, isn't it?
If I,
this particular first file,
see,
I kept 0,
1,
2 here,
so that you can make sure
that what is
a unique identity
in all these?
Through which,
I can find out that
this particular
click
or whatever it is,
this particular.
So,
what I mean to say is that
on whichever I am hovering,
one thing is
confirmed,
I know that
if this is 0,
then 0 will not be
of any other value.
If this is 1,
then 1 will not be
of any other value.
So,
a unique identity
in our case is
this ID number.
So,
the moment I click on this,
I will make sure
that
which list
the user clicked
and when he clicked,
what was
his ID number?
So,
I have to get
that ID number
and then
our task
in the task,
all my
lists will already be there,
all my tasks,
I will match
them with this
through loop
or through
any filter,
I will match them
and whatever I match,
I will leave that
and return the rest.
So,
see how it is done.
Now,
you know this.
What I will do in
deletion intentionally,
before writing return,
I am going to write
the case here.
Const,
updated,
state,
task,
will be equal to,
see carefully,
now,
make sure
we have this
task,
okay.
So,
again,
state,
.task,
I can never
directly write task,
because
state
is equal to
initial state,
.task,
object,
if you want to
access the property
we need to use
the . notation.
So,
state,
.task,
I have got the
data of the
So,
it is normal.
Let's go with
filter method.
Okay.
Here,
we have a
current task,
then we have
this index number
and a
return.
Then,
what we will do is,
whatever index
I have,
this index by the
way,
whatever index
I have,
now,
whichever
it does not
just return it.
And,
do not return
So,
now,
you have to think
that,
the user
who clicked on
where will
ID from?
I know,
you have the
same doubt.
Because,
there is no
idea
where the
ID is.
I mean,
user clicked
on this,
I know
it is
ID 1.
But,
how will
it be found here?
So,
here,
how will
it be found?
Let me tell you.
Whenever,
if you need
extra information,
then always
go with
action
dot
payload.
This is it.
You do not
have to think
You have to
paste action
dot payload
here.
Your work is
Do you know
why we do this?
Because,
I know,
when I will
on this delete button,
when I will
define the
function,
then I will
tell you,
when I will
dispatch this
I will send
action dot payload.
So,
we can get it
But,
for now,
you understand
like this.
Now,
I will
copy this updated
task and
paste it
entire file.
This is it.
So,
my work is done.
So,
we made
reducer function
for two actions.
First,
if someone wants
to add task,
then we
this case.
Second,
if someone
wants to delete,
then we
reducer function.
At the end,
we always
told that
no matter what,
you have to
pass new state.
So,
in default,
return
state as
it is.
This is it.
So,
yeah guys,
this is it.
This is what
our reducer
function actually
looks like.
And,
here only,
on the base of
action,
which operation
we have to perform,
we will make
sure.
Simple.
So,
guys,
in tomorrow's
video,
I will tell you
how to
create store
and then
how you
can
actions
or all
your particular
tasks.
I will tell you
in tomorrow's
video.
And,
it will be
fun.
But,
I hope
at least
you understood
State
and then
action.
State is
nothing but
your current
state,
which is
like your
initial state.
Never
mutate
old data
like this.
That's why
always use
spread operator.
And then,
second thing,
if you need
extra information,
don't think
Direct action
dot payload.
And,
we are writing
payload because
when I
go to dispatch
action,
basically,
when I
call this
particular case,
then I will
say
payload
equal to
this value.
If I write
thapa instead
then I will
write action
dot thapa.
So,
payload is
just a name
that you can
Else,
it is not
mandatory
to write
payload.
So,
yeah,
this is it.
this was
your reducer.
If you have
any doubt,
let me know
in the
comment section.
I explained
the whole
thing in
the previous
video.
guys,
what we
are going
to see
I have already
shared with
So,
if you
like it,
share it
with your
and
next target
is 7 lakh
subscribers.
If you haven't
subscribed yet,
then please do
so.
See you
in the next
video.
Till then,
guys,
thank you so
much.
Take care.
