Last video we discussed what is going wrong with our fenceposts action creator.
So now that we've got a better idea of what's happening here we're going to start to discuss how we're
going to fix this problem.
Now first a quick little piece of terminology up to this point in this course we have been writing what
we refer to as synchronous action creators a synchronous or sync action creator is one that when we
call it it immediately returns an action object with all the relevant data attached that object and
it's ready to be processed by our reducers.
We are now starting to venture into the world of asynchronous action creators a asynchronous or async
action creator is one that is going to require a little bit of time before it is ready to eventually
dispatch an action by definition.
Any time that you have an action creator that makes a network request it is always going to qualify
as an async action creator.
If you want to have asynchronous action creators inside of a redux application you have to install something
called a middleware that is going to allow you to deal with these asynchronous action creators.
And if you recall one of the error messages that we saw inside of our application made reference to
something called middleware.
And it said if you want to deal with async action creators you have to use a middleware.
So that begs the question what is a middleware.
Well as you recall a little bit ago we installed a dependency called redux thunk.
This is a middleware that's going to help us deal with this asynchronous action creator.
Now before we just run off and start making use of Reducto thunk I want to make sure that you have a
good idea of exactly what a middleware is.
So for the rest of this video we're going to have a quick little aside and we're going to discuss exactly
what a middleware is inside of the world of redux.
All right.
So first off same diagram we've looked at before with one tiny little change on it.
So we still are going to be calling action creators.
That's going to return in action.
That's going to be dispatched.
But now when we dispatched an action rather than sending it directly off to all of our reducers we're
going to say that action is going to be sent through all of the different middleware is inside of our
application inside a single redux app.
We can have as many or as few middleware as we want for the most part in the projects you and I are
going to be working on we are going to have at most one or two middleware is hooked up to our redux
store.
Let me tell you a little bit more about middleware is in detail.
All right.
So first off a middleware is a plain javascript function that is going to be called with every single
action that you dispatch inside of that function a middleware has the opportunity to stop an action
from being dispatch.
So prevent it from going to any of your reducers middleware can modify an action or it can otherwise
just kind of mess around in with an action in any way shape or form you can imagine a very simple example
would be to create a middleware that simply cancel locks every action that you dispatch.
So any time you dispatch an action you might have a middleware that is going to simply cancel log it.
So you can see that oh hey an action just got dispatched.
There are a tremendous number of open source middleware that you can install as dependencies into your
project.
Now you are not limited to only making use of open source middleware or installing them as dependencies.
We can very easily write our own custom middleware and use them inside of our own redux store.
One of the most popular uses of middleware is for dealing with these asynchronous action creators.
In fact probably the most popular middleware out of all of the ones that exist in the world is redux
thunk and like we said redux thunked is about helping you deal with asynchronous action creators.
There are many other types of action or me many other types of middleware but again many of them are
going to be focused around helping you deal with these async action creators.
So thats just a little bit on middleware in redux.
Now take a quick pause.
When we come back the next section we're going to start talking about exactly what redux thunked does.
So quick break and I'll see you in just a minute.