Why do we need middleware for async flow in Redux?

前端 未结 11 2468
忘了有多久
忘了有多久 2020-11-22 04:17

According to the docs, \"Without middleware, Redux store only supports synchronous data flow\". I don\'t understand why this is the case. Why can\'t the container component

11条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 05:04

    There are synchronous action creators and then there are asynchronous action creators.

    A synchronous action creator is one that when we call it, it immediately returns an Action object with all the relevant data attached to that object and its ready to be processed by our reducers.

    Asynchronous action creators is one in which it will require a little bit of time before it is ready to eventually dispatch an action.

    By definition, anytime 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 those asynchronous action creators.

    You can verify this in the error message that tells us use custom middleware for async actions.

    So what is a middleware and why do we need it for async flow in Redux?

    In the context of redux middleware such as redux-thunk, a middleware helps us deal with asynchronous action creators as that is something that Redux cannot handle out of the box.

    With a middleware integrated into the Redux cycle, we are still calling action creators, that is going to return an action that will be dispatched but now when we dispatch an action, rather than sending it directly off to all of our reducers, we are going to say that an action will be sent through all the different middleware inside the application.

    Inside of a single Redux app, we can have as many or as few middleware as we want. For the most part, in the projects we work on we will have one or two middleware hooked up to our Redux store.

    A middleware is a plain JavaScript function that will be called with every single action that we dispatch. Inside of that function a middleware has the opportunity to stop an action from being dispatched to any of the reducers, it can modify an action or just mess around with an action in any way you which for example, we could create a middleware that console logs every action you dispatch just for your viewing pleasure.

    There are a tremendous number of open source middleware you can install as dependencies into your project.

    You are not limited to only making use of open source middleware or installing them as dependencies. You can write your own custom middleware and use it inside of your Redux store.

    One of the more popular uses of middleware (and getting to your answer) is for dealing with asynchronous action creators, probably the most popular middleware out there is redux-thunk and it is about helping you deal with asynchronous action creators.

    There are many other types of middleware that also help you in dealing with asynchronous action creators.

提交回复
热议问题