search

Home  >  Q&A  >  body text

javascript - Doubts about redux asynchronous

In react, you can handle ajax asynchronous requests in hooks. Why do you need to use middleware in redux to handle them? Is there any difference between the 2?

黄舟黄舟2736 days ago596

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:38:40

    For a small WebApp, the state is relatively small. You can only use ReactJS to implement it, initiate an AJAX request in the event callback, and update the state of the ReactJS component after the request is successful, thereby updating the view.

    But in a larger-scale WebApp, there are many modules and many states. At this time, Redux needs to be introduced to manage the state of the entire WebApp. ReactJS is only used as the View layer and dispatches the corresponding Action according to the events triggered by the user. There will be no Business related logic.

    At this time, the Ajax request will be processed in Action, but Redux can only distribute Plain Object as Action by default. After adding the redux-thunk middleware, you can distribute the function as an Action, and you can do what you want in the function. Of course, you can also initiate Ajax requests to support asynchronous events.

    reply
    0
  • Cancelreply