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?
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.
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.