HenrikJoreteg / redux-bundler

Compose a Redux store out of smaller bundles of functionality.
https://reduxbundler.com
583 stars 46 forks source link

Question about using getExtraArgs for API Wrapper #52

Closed JayChetty closed 5 years ago

JayChetty commented 5 years ago

Love the approach taken by this library. Was wondering what the advantages are of injecting the API wrapper using getExtraArgs and using reactors, compared to having something completely separate that just subscribes to the store and triggers actions?

HenrikJoreteg commented 5 years ago

Hey! glad you like it.

Injecting the API wrapper with getExtraArgs does a few things:

  1. Makes action creators really easy to test. Rather than have to mock an API wrapper that you import you can just pass it into the function you're testing.
  2. Gives you a way to generically handle API errors in a way that has access to the store. For example, if you want to always dispatch a AUTH_EXPIRED action whenever the API returns a 401 response. Rather than handle that logic in every action creator that calls the API you can make your API wrapper handle it. (of course you could pass the store into your helper too, but this way it's just done).

The reactors thing is a separate question. Essentially the "having something separate that subscribes to store and triggers actions" is essentially how reactors work. That said, there's nothing stopping you from building your own using store.subscribeToSelectors. But the built in approach also helps a bit by only allowing one at a time, and uses requestIdleCallback to attempt to schedule the action for a time when the main thread is busy, causing jank, etc.

Hope that helps.

JayChetty commented 5 years ago

Helps a lot. Thanks!