Dispatching cascading/dependent async requests in Flux/React

佐手、 提交于 2019-12-04 12:50:04

I recently watched an informative panel discussion that had, among others, two Facebook developers working on large-scale projects that used React/Flux. What struck me was that they took completely different approaches to the same problem -- and both seem perfectly good.

That said, here's how I'd handle it.

  1. LoginForm.onSubmit calls LoginAction.login.
  2. LoginAction calls API.login and, upon success, the Dispatcher launches an actionType of something like Constants.LOGGED_IN with data of the user_id
  3. A UserStore, listening for Constants.LOGGED_IN makes a call to API.userInfo, passing the user_id that was in the dispatch. (Having a store get information directly from the API was one of the things one of the FB guys said they do routinely, reserving actions for updates and things of that nature.)
  4. UserStore saves info into its current_user and emits a CHANGE
  5. Affected components request an update to UserStore

Now, if you want to get trickier, you can have the store add arguments to its emitmethod, so that the affected components could pick up (a)the nature of what changed and (b)the actual data.

Hopefully, that's food for thought!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!