React Redux Tutorial #1: Why, When, and How to Use Redux in React

Thapa Technical
63 followers
2 Video Views·Oct 14, 2024
hello what's up guys and welcome to brand new react redux series in 2025 so believe me guys i'm
super excited and i hope that you are also excited because it's going to be a lot of fun
so guys first of all why redux what is redux before understanding it we need to understand
that why should we learn redux or why should we come okay so first of all guys see
redux is not a part of react, it is just an extra tool or you can say an extra library
but how and why right
so first of all see if you create any normal application in your react application
then believe me guys you don't need redux at all because
earlier redux was very famous and still is but nowadays we have react hooks
in which you have a context api and second is your use reducer hook
so with the help of these two you can easily manage the state and create a centralized store
that is creating a centralized store that holds all the data
and then they can do the code operation
so first of all this centralized store concept is what redux brings you
and then guys you know you don't have to use redux every time that's why for small applications
they started hooks in react in which you have context api and then use reducer hook
okay i hope you got to know this much if it is a small application
simple normal even e-commerce you can normally do it with the help of redux or both
it's totally up to you
but i highly recommend you to continue this series
continue this video till end you will get the full idea
okay so first of all guys i want to explain you the concept of the data flow in react
okay so you can see very clearly that our data flow is from parent to child
and then again parent to child okay
as
let's imagine uh we have this component right we have an app component then
parent and parent
execute
we have uh
grandparent
parent children
in react
This is called Prop Drilling
Prop Drilling is not a solution
This is also a problem
But atleast in this
Whatever data you feel
What is special in this particular component
That data is not with
Its parent or its grandparent
But only with this
Whenever something like this happens
Whenever we create an application
We make sure
What data is going to get
Atleast what we can do
Our app component
Our main parent component
We will keep all our data
There
Now this will atleast
If the data is at the top
Because how data flows in react
From top to bottom
I just showed you
You can pass the data at the top
So what we will do
This app component
It will pass parent 3
And parent 3 will pass it
It means
The data at the top
Atleast I can pass it
But again
There is a problem
The problem is
This data is kept here
And data was needed by parent 3
Why are we passing data to parent 3
Parent 3 didn't need data
But still
I had to pass it
Let's imagine
I make a box
Let's say this is D
Let's say it doesn't need data
It needs data
Now you tell
First it will pass data to parent 3
Then to parent 3
And to parent 3
So it needs data
Why should I pass data to parent 3
This is a problem
Why should I pass data to parent 3
To someone who doesn't need data
To solve this problem
Then we have a
Context API
Which is a hook
It is very simple
Let's say
Problem is solved
That is
Give a common parent component
So that
Anyone can get data
Right
But what did they do
If they give it to parent
Then child has to pass data
Then parent has to pass data
So we got rid of that problem
We have one store
Where all data will be there
If parent 1 wants
Then parent 1 will get data
If parent 2 wants
Then parent 2 will get data
No problem
This is a very awesome feature
But in reality
This is your redux
Yes guys
Context API and redux
Kind of both are same
Context API
Use reducer hook
Context API
Use reducer hook
Context API
Use reducer hook
Redux
Redux is also the same, which I explained earlier about Context API
Here also we have one centralized store
If you want data, you can take it
No problem, no dependency, no problem of prop drilling
If you want data, you can take it
Update it, whatever you want
So guys, this is Redux and I hope you got to know
Why Redux was made and to overcome Redux
React has made their own hook
So now let's understand what is Redux
So what is it?
Redux is a tool that helps to manage data also known as store
It's simple, whenever it comes to data in your large application
So you have to use Redux there
It allows us to keep all our
Apps data in a single place known as
Redux store. So whatever data you have in React application
It keeps all of them in one place
I am making it easy to share and update
Data across different parts of the app
I explained it to you earlier in the diagram
So that's it, you have to manage data but in a large scale application
You have to use Redux there
Else in small application, there is no need
Alright, so now
How Redux works?
There are many things to work with
But in simple terms, if I tell you three things
One part is store
Then we have action
And then we have a reducer
So you can say, a place where all your data is going to be stored
Obviously, you need data, right? Then action
So basically, this is an object that tells you what to do
What to do?
If you want to add, increment or decrement
What to do?
Reducer's job is to perform the action
How to do?
It actually changes the data in the store based on that action
So yeah, I hope you understood this much
But I want to show you
So guys, you can see this application
World best React version 19 series
It is a part of it, it is completed
I have explained the React query in the end
10 stack query, now Redux
So guys, you won't find a better series than this
Believe me, you won't find a better series than this
I have made this and I have live hosted it
Suppose, I write mango
Now, I am going to do add task
What am I going to do?
The moment I click on add task, the data is added
So what was this?
This button of add task is your action
Whenever someone clicks on this button, what do you have to do?
This particular action is that
I have to add a data
Now, how will I add?
With the help of reducer, I will write logic
So this is your action
Suppose, you are storing apple, mango, etc
But at least, you know what is action
In the same way, when you click on increment, decrement button
What do you have to do?
It will say action
I will explain in detail
Let's understand store
The redux store is like a big box
Where all your application data is kept safe
Everything you do with redux
Whether adding, removing, updating
Your data goes through this store
Whether you are adding, removing, updating
Everything is done through this store
This is one of the best part
Like in our redux
There is a centralized store
From where data is being taken
No one is dependent on anyone
You have a store, go and buy groceries
This store is not a big deal
I have already told you
This is your object
Which tells redux what to do
We have two properties
Type
And then we have one
Payload
Like you can see
What we are passing on type
State and event name
Basically type
Let's say there is an add button
Whenever someone clicks on add button
There is a counter
It tells what to do
If you want to pass extra information
We have sent something like this
This is a counter application
If you see second example
Instead of addition, we are decrementing
There are different ways
Let's say there is a minus button
Whenever someone clicks on this button
We have to perform an action
To decrement
We pass decrement by 10
This action tells redux what to do
And then we have something called
Reducer
If you see this example
It looks like useReducerHook
In useReducerHook
We have reducer function
Inside that reducer function
We have initial state and action
In action, we perform
Counter forward slash add
How am I adding
We do the same in useReducerHook
To perform this action
We have to call dispatch
Whenever we call dispatch
It will trigger this action method
And we check the case
Which case it is matching
User clicked on plus
I have to return updated data
If you click on minus
It matches the case
If you have used useReducerHook
You might have felt
It's so simple
But it's true
Action then reducer
How to connect react and redux
We have a useSelectorHook
SetState
There are 2-3 things
In redux, it was there
React team made useReducerHook
Here we have a useDispatch
How to connect react and redux
UseSelectorHook, SetState
Action creators
That's all
I hope you remember
Store, Action, Reducer
Let's talk about some advantages
No matter what
We have a centralized state management
Store your app state in one place
Making it easier to manage
Global access
Predictable updates
State changes are controlled
And predictable using reducer
Everything is predictable
You have an idea
If I update it
Will I get data
All actions are in pure functions
Even reducer
If you pass the same argument
You will get the same output
You understand this much
We have a devtools
React has its own component profiler
Redux has its own devtools
When we saw 10 stack query
It had its own awesome devtools
Redux has a powerful devtools
You can debug, inspect state
Replying actions
Async support
You must have heard about Thung
Thung handles async task
When you are calling an API
If you want to fetch data from any API
Whether it's an application
We used to use Axios
But in Redux
With the help of Thung
Thung and Saga
The middleware
Helps us to fetch data
Now we have a Redux Toolkit
Kind of boilerplate
We don't have to do much
When you use Redux Toolkit
First we will see Redux
Then I will tell you about Thung
Then I will tell you about Redux
Then you will get an idea
Don't worry
If you don't use Redux
Why should I learn it
Until you know the value
How can you give importance to it
What was there in Redux
Because of which you had to make a Redux Toolkit
Is the Redux Toolkit better or not
If I use Redux
It will work
If you are working on a big project
You may not have used Redux Toolkit
You may have used the old middleware
Then what will you do
Yes, I know Redux Toolkit
But when you don't know Redux
Then what is the use
They just need Redux
If you know Redux
Then you will get the idea of Redux Toolkit
This is it
I want to tell you my final verdict
If you are not working on a big project
If you are working on a small project
Go with normal React
Use context API
And use Reducer Hook
But when it comes to a large-scale application
Where a lot of team members are working
When you work, you will know
Then you must use Redux
Then you will know its value
Pick up an e-commerce website
Make it with context API and Reducer Hook
Which I have already made
You can check out that video
Check out the Redux part
Once you understand
Then pick up the same e-commerce website
Now instead of using context and Reducer
Remove it and make it with Redux
If you can do that
Then tell me openly that you know Redux
Else I will teach you an example
I teach you so that you get it
But then if you do it well
Then I will believe that you know Redux
So this is it
I hope you enjoyed it
If you like the video, please like
If you have any doubts, please comment
I am always here
Subscribe
See you in tomorrow's video
