4

In the flux architecture, is it common practice to grab data from a store in an action creator? If not, would that mean that it's better to pass all needed data for network calls in through the component params?

I have an application that has a 3 level deep component, and just wondering how realistic it is to copy data from level 1 to level 3.

Any explanation would be greatly appreciated.

kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
BJacobs
  • 122
  • 1
  • 8

1 Answers1

6

It's fine for the stores' getters to be called in the action creators, but usually the action creator will call a WebAPIUtils module, where the actual call to the stores' getters will be found.

I would question the practice of passing anything through the view layer that isn't actually used by views (usually React components).

Network calls are usually made within a dedicated utility module. These are sometimes called DataLoaders or WebAPIUtils modules. They differ from other utility modules in that they often pull data out of stores before making the network calls.

Other utility modules should be libraries of pure functions, with very few dependencies, if any. This keeps them very portable.

fisherwebdev
  • 12,658
  • 4
  • 28
  • 27
  • How do you decide when to put logic in the action creator, the WebAPIUtil or the store? Where should caching logic (e.g. only fetch from server if data isn't already in store) live for example, the store? – Markus-ipse Mar 11 '15 at 20:17
  • 1
    For the store vs. action creator question, I think it's mostly a question of style, and whether you trust your team to not make the mistake of handling the response directly in the store. If you put it in the action creators, there is no way to make that mistake. Choose one approach for the project and stick with it. For the caching, I've seen different approaches, but all of them obviously need access to the store data, either directly or through getters. Generally I like to keep everything related to XHRs in a Utils module and just call it from either the store or the action creators. – fisherwebdev Mar 20 '15 at 16:03
  • @fisherwebdev How do you ensure that the store has the correct data available when you need it? What happens if the ajax call that populates the store hasn't finished yet? – kevinrutherford May 22 '15 at 19:36
  • XHR calls are typically handled with three different actions: STARTED, SUCCEEDED, FAILED. I'm not exactly sure what you mean about ensuring "the store has the correct data available when you need it", but the view layer simply displays what is in the store. If the XHR is originating with user input, you can do optimistic updates by displaying the data that came with STARTED and then confirm/rollback after SUCCEEDED or FAILED come along. Alternatively, or if your XHR is for initial data, you can use null values as placeholders and simply show a spinner if your data is null. – fisherwebdev May 23 '15 at 11:25
  • I would encourage you not to use XHR for initial data if you can help it, and instead try to get the data included with the initial page load, just for the sake of a better user experience. Then you can take the data and dispatch it in an INITIAL_DATA_LOADED action, before rendering. But if getting the data with the page load is really not an option, I would use nulls and spinners. – fisherwebdev May 23 '15 at 11:31