S
S
Sergey2020-09-18 08:31:33
React
Sergey, 2020-09-18 08:31:33

What is middleware for?

Why is it impossible to make a request right inside the component and push the data into the state?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Alexandrovich, 2020-09-18
@justedoit

Why not? can. But you must understand that as soon as the component is unmounted you will lose data. Therefore, requests are usually made in redux actions and the received data is stored in redux. Thus, as long as you have convenient access to the received data from different components + you do not need to re-request because you already have them in redux.
And middleware is just a layer between the dispatch and the moment when the data will be put into the redux. At this point, you can, for example, add some more logic. For example, check the server token for the fact that it has not yet expired, and if it has expired, throw it out for authorization or make a request for renewal.

V
Vovash, 2020-09-18
@V0vash

For example: there is a conveyor along which the coat moves. Zina and Lyudmila work on the assembly line. Zina sews on a button, Lyudmila attaches a tag. Suddenly, the middleware Lena appears, stands between Zina and Lyudmila and paints the button in a hipster trendy color. Since Lena does not take the coat with her after painting, but passes it on, Lyudmila, as if nothing had happened, attaches a tag and the coat is ready. Only now it's hipster. Reinforced.
Read here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question