javascript - Doubts about redux asynchronous
黄舟
黄舟 2017-05-19 10:36:40
0
1
623

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?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(1)
伊谢尔伦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template