The problem of multiple fetch requests failed to jump in a page of react

scenario:

     fetch  tocken token  

question:

    fetch   async await 
Mar.21,2021

my solution is to use high-order components + redux to solve the problem.
first of all, all the interfaces go to redux, where the data is normal, where should dispatch go to dispatch. If login fails dispatch A failed login action, then handles the login problem in the high-level component, if the login state is not found, then jump to the login page and do nothing if there is a login state.
Note: do not dispatch, the login page itself in reducer, do not be wrapped by high-level components

< hr >

and then there is the question of aesthetics. WithRouter (connect (state= > state) (LoginHOC (YourBusiness);
is just a little longer.

< hr >

in fact, I just casually answer, redux management interface, it is true that the page view can be completely decoupled, but you know how troublesome it is to hit a toast with an interface exception. Dispatch to redux, in a few seconds and a cancelToast in dispatch

< hr >

I'm also waiting for better interface management. Sometimes contradictory reuse lines think that this decoupling, the introduction of a tripartite state and subscription, and then map to their own state, uncoupling will certainly cause inconvenience.

ps: some colleagues in the brain circuit who are light and strange, let the ajax module accept a this, and then be able to manipulate the page. Oh, my God. That's not as good as Vue's mixin idea.


1. Login will not be returned many times. If the first request you make determines that token is invalid and adjusts it to the login page, then none of the subsequent requests will execute
2. Multiple requests may have been made before returning, but the return will only be executed once


I guess your scenario is client routing and fetch initiated by multiple components on the page.

it is recommended to encapsulate the fetch function as a public object, and each component can call it uniformly, and the identity exception can be handled uniformly, so that it can be easily handled when the second exception is about to jump. The easiest way you can set a flag, the first jump behind the true, do not jump. Then the identity is processed and the flag is set to false.


Let me add that finally promise.all can basically solve this problem

Menu