1

I'm porting a big Reduxless React application to Redux and I'm not sure how to handle initialisation of the app.

At start the app fetches all the URLs required for it to work so every other fetch after the first one actually uses these URLs. During the lifetime of the application itself they never change, they're all static yet I have to get them dynamically because they might change from time to time.

So I'm fetching, let's say, 150 URLs, and here's my question - where should I store them?

1. Redux

Putting the URLs to Redux storage makes sense at first but then I noticed I'd use these urls in action creators almost exclusively and it's considered an anti-pattern as far as I know (see top rated reply here why). I even tried accessing them in action creators but it was a hassle.

2. Global variable

Making the first call outside of Redux and storing my global variables in an easy to reach place such as window.MyAppNameConfig.urls makes sense but what if I dispatch some Redux actions before the global variable is set? I'm doomed. Maybe I could check somehow if the variables are set and then repeat dispatches? But it sounds quite complicated.

3. Redux + global variable

Maybe it would be possible (I'm using redux-axios-middleware) to dispatch getTheUrls action and then instead of pushing it to the store - save it as a variable? Not sure if this is a good idea and I'm not really sure how to achieve that (I'm only learning Redux), if no, why not?

Thanks for any input.

Wordpressor
  • 7,173
  • 23
  • 69
  • 108
  • I think "side effects" in redux actions doesn't matter whether its getting redux state or some other global variable. and accessing state inside an async action creator is completely normal and very common. that's why redux-thunk passes `getState` along with `dispatch`. as for 2) same problem happens with redux state.. either way you can delay rendering of anything that could possibly fire an action, like the entire app even, until the first call comes back. in any case, both a global module (not necessarily window) or redux state is fine. don't overthink it – azium Nov 26 '17 at 01:10
  • why not use one URL (globally?) that will fetch from the server every config that you need? i assume that only the domain would change and not the server method actions? – Sagiv b.g Nov 26 '17 at 01:38
  • are you getting the list of links from another link? In other words, is fetching the list of links an async operation? What I'm thinking is, you can delay the rendering of the app until you have fetched the list of links. Run an action that fetches the links and when the call finishes, run another action to set a "done" flag to true. – Sammy I. Nov 26 '17 at 03:53

0 Answers0