0

I'm developing a react native application using flux which has a number of components and a number of stores. The application uses a javascript SDK which authenticates once against an online rest API. The SDK object returned is then authenticated for any future calls.

I can either call the rest API from actions or stores. How do I share that authenticated SDK object against a number of stores or actions? And which of the two places are best to call the API?

tooba
  • 551
  • 5
  • 22

2 Answers2

0

Use a Singleton. If you are packing your modules with Browserify or Webpack, it should be simple to make a module exports object which encapsulates the SDK interface.

Wylie Kulik
  • 696
  • 5
  • 17
0

The Singleton approach is how it should be but it also should have some architectural pattern :). In fact the logic for creating API call, authenticating etc. should be neither in action itself, nor in the store.

In the flux architecture the guys from Facebook introduced also some "helper utilities" called Action creators - those are responsible for creating the right action and passing it to the dispatcher (they might need to have some web api call behind to get the action). A very nice explanation of this part of Flux is available here - including visual explanation:

http://facebook.github.io/react/blog/2014/07/30/flux-actions-and-the-dispatcher.html

So - all in all - have an ActionCreator singleton to prepare the actions for you and use it in your views in the place where you want to send action to the dispatcher.

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61