5

I have Redux app with React Router (based on https://github.com/este/este).

Inside one Route, there may be more than 1 AJAX calls (fired by redux-promise-middleware & redux-thunk). When the page changes (via react-router) I wish to reject all remaining _SUCESS or _FAILED callback actions fired by the previous route.

What is the best way to do this?

Pahlevi Fikri Auliya
  • 4,157
  • 8
  • 35
  • 71

1 Answers1

0

I'd suggest that you make the data you fetch page-aware. Meaning that in the action where the fetch is started, add a page-context. When the reducer gets the data it can either save it for that page-context or it can throw it away if the location is not the same as your browser (meaning that the user has navigated away). If you keep the data for the different pages/contexts you also have the bonus of these being ready if the user returns (if that is something that you'd want).

  1. You are on url "/pageX". You start fetching data and the action makes sure that the page-context is remembered for when the SUCCESS action is to be dispatched. When the reducer handles the action it stores the data in store.context["/pageX"].data (or similar). Note: This is where you could also throw it away (reject) in case the current location is not the same as the received data.

  2. The UI should know how to ask/use data from the context that matches it's location only.

You might also want to consider tracking the browser-location in the state for the app...

Spiralis
  • 3,232
  • 2
  • 39
  • 53