DISCLAIMERS: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.
Hello everybody! In this episode we are going to learn about redux, which is a state management library. We are going to use redux with react to help us grow and maintain larger apps.
We are going to be using react-redux which now offers a few new tools that easily integrate with react hooks, such as useSelector and useDispatch.
I highly recommend you to watch my react for beginners video if you are having trouble following this tutorial.
Good morning my gorgeous friends on the internet. In this episode we are going to talk about Redux.
All right, Redux. What is Redux? Redux is a state management tool that we can use in our projects.
So we can use it in React, we can theoretically use it in different frameworks as well. Now,
do I like Redux? No, I don't like Redux. And the reason why you're going to see a lot of people
being like, uh, is there's a lot of boilerplate that might be going on into your projects to do
a very simple thing. And there's also a lot of new jargony words like dispatch, map state to props,
combine reducers, store, is Elvis Presley dead? What's the point of life? So personally I'm not
a big fan but a lot of jobs really use Redux because when you have a large scale application
you're going to have some state management tool and Redux being the most popular one with React
that's the one that they are looking for. So I want to show you all the basic things so you
understand how everything works so in case you're ever going to get hired or maybe even use it on
your personal projects but personally I don't think that's really necessary because you can
get away with context or just passing down props in React. Oh and lastly thank you everybody who
that's quite crazy I haven't even made a video about it so thank you thank you so much if you
want to support me there's going to be a link up here somewhere you can click it and you're going
to have access to I'll try to put up as much as possible extra projects for you to make tutorials
discussions and source files and discords and there's a lot more so check it out up there and
thank you thank you again so much. Okay let's get started first of all I actually want to show you
what Redux solves for us so in React I'll throw up Figma here oh look at this design I just made
it very awesome okay so we can imagine that we have this app component okay this is all React
okay so we have this app component and imagine that we're making a movie searching app so you
would actually create a few components to kind of seclude the logic into these small components so
maybe you have a search component that only searches the movies for you you will have a movie
list that displays all the movies you have a login and then maybe a individual movie all right so
from this movie list you would have another component that displays each individual movie
all right so just basic basic React app here all right so what we learned is how we can pass data
from one place to another so you can imagine that we would have the data all right so the state of
our movie list in here all right so if I want to have the movies I would probably put it in here
okay there's no real logic of having the list of movies so the actual data in my login okay
so let's take this data I made this a circle because that'll look cool all right so if I
want to have the movie data so all the movies I would put the data in here okay very cool now this
way I can render out in each individual movie in this movie component so I can have this data
and if I want to pass down some data from this component to this component we learned that we
can do that with props so I can pass some data down here all right the problem is that we cannot
pass data from our movie list to something like search all right that's not possible
react only flows in one direction which is from up here all the way down all right so if I want
to pass some data from the movie list I can only pass it down here so whatever you render inside
this movie list in this case we render the movies the movie and individual movie that's the only
one that we can pass data to so how can we solve that problem maybe I want to have access to this
data with the movies inside my search so whenever I search for something I can examine that data
and return a new movie list so that's the first problem here and the way you would fix this
is called moving up the state we're lifting up the state I don't know what they call these days
but the idea is that you don't have the data about the movies inside the movie list you would put it
up here to the app all right and that way you can pass it down to the movie list if you want
you can have access to it in the search if you want so again you can just pass it down in props
and now you have access to all the movies inside the search okay and same goes here so I can pass
it additionally down here and here maybe I can I can choose not to render things out if I'm not
logged in okay now the problem is here the more components you have the crazier this is going to
get okay and also why would we have the app the data with the movies in our app here that
doesn't really make any sense okay usually we want to have all the logic inside the components
that really uses it so movie list and here so that that's kind of the problem that we're trying
to solve also imagine that we also want to have an action so maybe down here the movie
I want to delete a specific movie so that means that we would also have to have a function
all right that deletes the movie from this data state so we would also have to pass that all the
way down here and then delete it and that's going to affect here so it goes really crazy really
quickly so what Redux does for us is we don't add the data in here in the app or the movies list
all right you can imagine that we kind of seclude this data into its own place all right so this is
a whole brand new place I'll drag it out here and we cannot see it thank you Figma you are really
awesome so where is that where is that thing grab look I cannot grab it anymore this is incredible
but you can imagine that we have our data now stored completely separately here all right here
perfect and now this way whenever I want to use the data so this can be the movies list this can
be about anything I want all right so we store the entire states separately here and then when I
whenever I want to access something I can just go to the component I need the information from
so maybe the movie list and I can just add a bit of code and then I can pull out the necessary data
that I need only in this component so I don't have to pass down things in props and change the
data and things like that so this gives us access to a way of accessing data in the component also
changing the data and things of that sort so that's kind of how everything works now let's
actually set this up in VS code for it to all make sense so I have an empty project here so I just set
up create react app if you want to set it up to all you have to do is run npx create react app and
then give it a name in this case I gave it learn redux so run that once it runs it's going to
create you this new folder here all right and this is just an empty react project and also what you
want to install is npm install react my apologies redux and react dash redux like so okay so redux
is the actual state management package and react redux gives us the ability to connect react and
redux together okay so let's get rid of this make sure to install that I already did so I'm not going
to run it again so as you can see everything is installed this version is 7.1 which is really nice
because it's going to give us access to use react redux with hooks okay I talked too much so let's
let's let's kind of let's oh my god I cannot speak let's do this from scratch I'm going to
delete a few files here that we're not going to need so open up your source file here and I'm
going to get rid of app.test.js we're not going to need these uh let's also get rid of app.css
that's fine let me mute this okay the logo we don't need the logo and let's go into app and just
get rid of everything I'm just going to leave an empty just hello okay just for now we're just
going to set everything up here in index.js I'm going to show you the basics of it and then we're
going to hook it up to react to make everything work just nice so the first thing I'm going to
need is that thing called store so store now what is the store the store is basically the state
a globalized state so everything I want to have take a look at a figma example here so the store
basically holds all the data or state for our application so you wouldn't have a data of state
only with the movie list here like you would normally in react so I would have the movie list
here maybe some information some data or state about the login here what you would do is you
would separate everything and you would have one big state that you can access anywhere and all
your components so you would separate everything you would have all all your data in this big store
okay so that's all it is it's just a big data here that holds all the information and state
for our application okay so that's the store so a globalized globalized state okay the second thing
that we're going to need is something called an action now action sounds like you're going to do
something but it's not really in the case of redux so what an action is it kind of describes
what you want to do all right so you can imagine and this one we're going to keep it simple we're
just going to build a super simple state that's going to be called counter and it's going to be
set to zero and whenever we add maybe we hit the button plus it's going to increment by one
all right so an action would be called increment all right it's just a name you just give it
you just give it whatever you want to do so you just say hey my action is increment so that's
we're going to create here increment and there's also another one called reducer and what a reducer
is it basically describes how your actions transform the state into the next state okay
so what's going to happen is that this action is going to be called the reducer is going to
check which action you did and based on the action it's going to modify our store up here
all right see how this is get gets a bit really confusing so don't worry if you don't get this
straight off we're going to write a bit of example code here and then i'm going to explain everything
and there's a final step called dispatch and this is the way where we can actually
execute that action all right we can say hey dispatch this action to the reducer so we can
send that action to the reducer the reducer is going to check what to do and then the store
gets updated okay cool all right let's take a look at how we can do this because it's very hard
to explain this with theory so the first thing we want to do is create this store so how can
we do that well we can import it from redux so i'm gonna say import create store make sure you
add these curly braces here from redux okay that's step one you create the globalized state
perfect now this state is going to take two things actually it's going to take one thing
it's going to take a reducer so we're not going to create the store yet this is how we would create
it we would add a let store and set this equal to create store and you would pass in a reducer
so reducer all right and the reducer that we're going to make is going to be called counter but
we're not gonna do this yet okay so you remember how i said that an action just kind of describes
what you want to do so imagine you can go to a store and you're like i'm hungry all right so
that's the action doesn't mean you're actually buying the food you just describe what you want
to do i'm hungry all right uh so we're gonna do this increment for now so let's create an action
how does an action look it's super simple all an action is is just a function that returns an
object okay a simple function that returns an object so if i want to increase a counter i can
just create a const and i'm going to say increment it's going to be equal to an arrow function like
so and all this does again just remember it just returns an object so i'm going to add a type to
it or you can do name so what's the name of this action it's going to be increment i'm going to do
all in uppercase usually you're you're gonna see something called type here that's what people name
them but it's actually just like a name all right so what is my action name increment but you're
gonna see type so i'm gonna keep the type and yeah this is what an action is now we can create
a reducer that's going to take care of all the actions related to our counter so in theory we
can do another one here so we can do decrement all right and the type is going to be decrement
here okay and now let's set up a reducer so again this is just going to be another function
so i can set up a counter and what this is going to take again just an arrow function that's going
to return a piece of object so here what we can do well first of all we need to add two parameters
one's going to be how is our counter state going to look like so what's the initial state of our
counter so i can just set state equal to zero for now we can also create it separately in a constant
or let here and then pass it in here but i can do it in here as well so that's fine and the second
thing is it's going to take in an action so now what we can do here is we can switch the action
depending on its name so i can say switch action dot type so it's going to take a look at the name
all right so either increment or decrement and based on the name of the action
it's going to do something for us so let's open up some curly braces here
and we're going to say case if our case is increment then i want to do something here
and what i want to do is i want to return state plus one all right so we're going to take a look
at the initial state which is zero and then we're going to add one to it okay that's all it's going
to do and once we do this it's going to update our store so hit save on this and then we can
have another one if we want so let's do case decrement all right because we set that up as
well so what we can do here is we can return state minus one if we want hit save all right
so this is our reducer now we can actually add it to the store so store let's say let store
it's going to be equal to create store and we can pass in this counter
okay and finally remember to actually execute that action we need to add
dispatch but before we also do that let's display it in the console so to display it
we're going to do this once we're going to say store dot subscribe and we're not going to use
this in react redux so you don't have to worry about about it too much but this is just going
to take an arrow function and we're going to say console log store dot get state like so all right
and this is just going to display our store for us okay so nothing's going to happen yet so to
actually dispatch an action we're going to say store dot dispatch and all i have to do here is
call one that i want i can say increment so i can say store dispatch the increment i'm going
to execute this and that's all i need to do so let's execute this and then we're gonna
see what's up npm start all right let's wait for this to open and then we're going to recap and
then we're going to implement it with dev tools and everything so everything is going to make
okay so let's see if this loaded up here let's open up our console and let's go here
all right it's still loading come on boy why you do this my throat is dry see this is why
okay take a look we have one all right we got this message here because we haven't used decrement
but we have one okay so let's take a look at what we did again what we do is we create the store
which holds all of our state we have one piece of state now which is counter you're going to have
multiple reducers depending on what you want to do you can have a authentication reducer that's
going to take care of authentication you can have a movie list reducer you can have multiple
that we're going to have in our big global state but for now we have one okay what we do is we
dispatch an action all right so an action all it is is just a function that returns an object
with a type increment so it just has a name to it all right and once we dispatch this
our reducer it's going to take a look at what was dispatched which action was dispatched it's going
to take a look at the name so that's why we added switch action dot type name it's going to take a
look at the name and based on the name it's going to return a piece of state so in this case
our zero was the original plus one and that's exactly what it did if I want to decrement I
can do it again I can say store dot dispatch decrement all right I can maybe do this another
time so two times let's take a look at what's happening so let's not this one
oh my goodness google chrome as you can see we have minus one now all right so we dispatch this
two times now the thing is uh you kind of set this up once but as you can see it kind of took
a ton here for us to set this up just to do so little so this is why I'm saying that
redux can have a lot of boilerplates and when whenever you have a small project and it might
not be worth it okay but let's set this up with react and see how that works so let's start from
scratch and hope everything is going to make more sense because with this I feel like just exercising
and practicing this is going to make it easier for you okay and we're also going to add one
additional thing so this is how I usually do it there you're going to find different ways that
people do it but usually I like to add my store in here in index.js and then separate each reducer
and action in individual files so first of all what I'm going to do is I'm going to create two
folders here I'm going to create one called reducers and one called actions so new folder
actions okay perfect so now what I want to do is I want to create my reducer so here I'm going to
create a new file and I'm going to call this index.js okay and then I'm going to create two here
so let's imagine that we have a counter and let's imagine that I also have a another reducer that's
going to take a look at if I'm logged in or logged out or maybe a toggle of a nav bar so let's create
a new file here we're going to do kind of the same thing here I'm going to say counter.js
all right now let's make another one new file and this is going to be let's say is logged.js
all right so we have two so let's set up the first one and we can kind of we could have copy pasted
what we had but let's just make it again so I'm going to say const counter reducer it's going to
be equal to I'm going to set in a state of zero and an action okay and we can switch the action
dot type you know which is the name and depending on what name it has we're going to execute
different things so I'm going to say case of increment what I want to do is return a piece
of state so state plus one all right and let's do decrement as well case decrement is going to be
return state minus one perfect all right so this is all we have then we can export this
default counter reducer okay let's also set up another one for our is logged in so let's exit
this let's go to is logged in I'm going to create const let's name this log to reducer
set this equal to state and action all right these are always the two things that we're going to take
in and for this one I'm going to set the initial state equal to false all right so whenever our
application first runs I want to make sure that we are not logged in so this is going to be false
again kind of we're going to do the same thing over and over again action dot type and we're
check the name of it and return a different piece of state depending on the name so in this case
I'm going to just have one let's say case sign in like that and when that happens what I want to do
is I'm going to return the opposite of state so it's going to be true now all right or optionally
what you can do is just say true here all right you can also return that but what I want to do
is say the opposite of false all right so that's it then we're going to export default
logged reducer all right so we have two pieces of state which are our reducers how can we hook
that up to our store okay we can go here and we can import our store so we can say import create
store from redux okay that's step one now we can say const store is equal to create store
and then we can pass it in but we have a problem here all right we have two reducers
and we're gonna pass in here counter reducer and the is logged reducer that's not gonna work
so another concept that we have is combined reducers so all that's gonna do is it's gonna
take all your reducers and it's gonna combine them together into one reducer that we can
actually add to our store so let's remove everything here and what we're gonna do
is we're gonna add a comma here and we're gonna say combine reducers okay and actually I created
a new file here in our reducers which is called index.js so we're gonna add it there just to keep
this index file a bit lean so let's go back into our reducers index.js here and what I'm gonna do
is I'm gonna import all the reducers here import the counter reducer from counter import let's say
is logged you can give any names you want here let's say logged reducer since we exported this
as default all it means is that we can pretty much give it any name we want from dot slash
is locked all right so these are the two now we want to combine it so we're going to import
from redux we want to import combine reducers this is the one and now I want to create a
you can do const all reducers or you're gonna see root reducer whatever name you want to give
to signify that you have all your reducers in this one constant here so all reducers I'm gonna
call it like so just to keep it simple and here all I'm gonna do is call combine reducers
and then pass in each individual reducer so it's going to be a function with an object in here
and here what you can do is you can add a counter reducer or you can give it any name you want
counter is going to be my counter reducer all right so the name that you're giving it in here
is going to be whatever you want so you can give it whatever name you want and you can access it
by this name and this is the actual value that's coming from our reducer all right so this can be
anything you want you're gonna see a lot of people do something like this counter reducer
only one thing and all that means is that that's just some es6 syntax sugar here and all it means
if you add one it actually means this so you have it both one here and one here all right but you
can actually shorthand this like so but what I like to do is actually give it a name that I like
so I can say counter is going to be the count reducer and then I can add a comma and logged
reducer is going to be let's say I want it to appear as is logged and it's going to be
logged reducer so I actually want to show you how everything in here looks so we can visualize it
all right so we have everything stuck in here we're going to go back here and import this all
reducers now so we can stick it in here oh okay import let's go to where is my reducer so we can
go to all reducers or yeah all reducer from dot slash reducers and notice here I don't actually
need to add index because webpack is automatically going to look at the index.js in this folder
so when you have index you don't actually have to add index in here so once we have this we can add
all reducer in here and we're done oh jesus okay we're done and this is just a setup by the way
we actually haven't done anything yet okay I want to visualize this so we know what's going on
so what we can do is we can import a little cool dev tool from chrome so we're having an error here
reducers does not contain a default export all right so we actually need to export this all
reducers export default all reducers perfect okay save that let's go back here
let's see do we still still have that error all right here I messed up counter reducer let's go
back here let's see where is my counter reducer it's here remember this is just a function and
I messed it up so let's make sure it's a function okay so make sure we add that arrow function there
and also let's go to is logged in and add a default case here so default if nothing happens
I just want to return the state so whatever form our status in this case is false just return false
here uh state all right so hit save and there we go uh okay so let's take a look in our index.js
we added it to the store and I actually want to visualize everything in my browser so we can use
an extension here which is very cool it's called Redux DevTools extension all right and we can
install this we can go down here we can add it to chrome so let's go here to chrome and Redux
DevTools you can install this and once you do that we can add it to our project if we scroll down
here and we can just copy this in here so basic store we copy this and all we do is besides our
reducer we have a comma and we just insert that thing in there okay perfect uh let's take a look
if we actually see anything so in here our app we're gonna press f12 that's gonna give us this
and we can go to our Redux there it is and take a look if I hit state here it's gonna show
everything we have in our state so we have a counter which is set to zero so if we take a
look again I kind of want to show you this multiple times until it kind of makes sense
so here's our reducer again so as you can see we have these multiple reducers counter reducer
that's one piece of state we have is logged in that's another piece of state and then we use
combine reducers to combine everything in here so as you can see counter is going to be my counter
reducer so counter you're going to see hey that's counter the name we gave I can optionally change
this to my count all right so we can give any name we want here and that's gonna appear in our
store as you can see my count so I'm gonna keep this counter for now but hey the counter has a
piece of state which is state zero so as you can see that's the value that appears here to the
right same goes with is logged in is logged in has a state of false all right we gave it a name
so is logged is gonna have that state the logged reducer state take a look it's false all right
so hope this kind of makes some sense and this combines everything together and we have the big
state okay very cool uh what we want to do now is actually hook it up to our app all right that's
what that's why we're here and how can we hook this up so what we can do is we can say we need
to import something from react redux so we can say import from react redux and what I want to
import is something called provider all right and what this does it just connects our global
states our store to our entire app down here so it's this is actually very simple all you have to
do is just make some space here and wrap everything inside this provider so all your app your whole
app now has access to the store so this only takes one argument which is store and we can just pass
in this store all right so this is like my store and it takes in one parameter store my store all
right but I gave it the same name just to keep it simple so store okay that's it now we have access
to everything inside our app okay so let's take a look how I can display maybe I want to display
the counter so how can we do that I'm going to close everything up here like that okay so you
can do this from basically any component you want any component you want to access you can run this
so all you have to do is to access a piece of state so maybe I want to use again I just want
to use this let me close this one we don't need that anymore maybe I want to access the counter
okay so what I can do is I can import something called use selector from react redux okay and now
in here let's say I don't want to say hello I want to say counter and here I want to add the actual
number of my counter okay so what we can do is we can say use selector and then I can extract that
counter from my state here I can extract this I can say cons counter equals to use selector
this is going to be a function here and I'm going to have access to the entire state in here so this
runs another function here and I have access to this whole big state all right so this state up
here I can have access to everything so I can just pull out state counter from here and that's it
now I can display it here if I want I can say counter oops too much view counter hit save
take a look counter is zero at the same time I can also extract is logged in if I want maybe
I have some sensible data here let's say I have let's see down here I have h3 that says
valuable information I shouldn't see if I'm not logged in okay and in here what I can do
is I can extract that is logged in if I want so this one I can go up here and say const is logged
I can set this equal to use selector again just state and I can extract state dot is logged
so we're gonna write that is logged and boom we have access to it if I want all I can do maybe
I can add a conditional statement here and say hey if is logged like so then I want to run this
html here else I don't want to see anything so I'm gonna pass in an empty string okay so
is if this is true this is gonna show up if it's not then nothing's gonna show up and as you can
see nothing shows up because r is logged is false okay now how can we modify this so how can we
increase the counter I'm gonna add two buttons here button is gonna say plus and I'm gonna add
a minus as well so minus okay very cool but how would we actually increment and decrement this
we have our plus minus here but nothing works so we would create an action for that and when we
click it we dispatch that specific action so in my actions I just made an index.js here it's empty
and this is going to be very simple all we're going to do is create an export const I'm going
give it the name of increment all right and all this does is it's going to have it's going to
return an object again with just the type of increment all right so that's going to be the
name of it and remember when we dispatch this this is going to go into our counter here it's
going to check what type of action type all right the type is increment so it's going to return this
piece of state state plus one okay so this is all we have just a function again that returns an
object good so in here I want to execute that so what we do is we import our action so I'm going
to say import increment from our actions so dot slash actions all right again we don't have to
add index because it's going to automatically know and then one more thing I'm going to import
from react redux is something called use dispatch all right this gives us the ability to dispatch
an action and in here I'm going to say cost dispatch is going to be equal to use dispatch
like so all right in here I want to add an on click so whenever we click I want to dispatch
that function so I'm going to add just a callback function here I'm going to say dispatch my
increment all right so I want to execute this increment like so run this let's take a look back
here it's increment let's not misspell that and everything seems to be working all right let's go
to our state as you can see it's still zero but when we hit plus look it updates to one so that's
very cool boom boom boom five six seven and everything works the way we want we have the
difference here which is really cool it changed it shows us what changed so let me refresh this
as you can see if I have plus it's going to say oh our counter increased from zero to one
and as you can see here it also shows us the function that the action that ran we do it
multiple times I can even jump back to one if I want to see I can jump back here to see my state
in this position and yeah there's a lot of cool things that you can do with this dev tool it's
really well made you can also play this back to see what you did take a look very cool okay
um so that works let's also hook up our decrement to do that we can just go back to our actions
copy paste this down here just add decrement and decrement like so app.js we import the decrement
and kind of copy paste this so let me just delete this copy paste this down here and just change
this to decrement and everything should still work the same way plus plus plus minus minus minus
yep everything works perfectly okay lastly I want to show you this what if I want to have an
argument here so I want to have an actual piece of data maybe I want to increment this by five
like so so what you need to do is let's see increment by five it's safe I'm going to go back
to my actions here and I'm going to add something else to this so now it takes a parameter here we
so it's going to take let's say maybe a multiply or the number I want to change it by so maybe it's
going to take in a number of increment so here what you can do is add a piece of data and you're
going to see that people usually say payload to this so I'm going to return also the number I
want to increment by so I'm going to add number here so now in my reducer I'm not going to return
state plus one what I can say is I'm going to return state plus action.payload all right so
this five so zero plus five and then it's going to be five it's going to say five plus five so
let's take a look and take a look now we are incrementing by five okay okay oh I talked so
much all right it's time to go so sing me a goodbye song all right I'll let you sing
sing the goodbye song now walk away
goodbye my love goodbye to my friend