简体   繁体   中英

React child component directly invokes Action in flux app - bad practice?

I am a JS novice and developing a flux app by scratch.

Currently the root component has Action instance, and child components are assigned with event handlers that invoke an Action. Deeper descendants are assigned with handlers that eventually reach Action, but it makes me reluctant to maintain the callback flow.

I made a static resolver to provide the access to Action like:

var action = Resolver.resolveAction(); // Returns singleton action

In this way, child component can directly call Action and emit through shared Dispatcher. Only the root component is listening to Store and refresh the whole component tree.

I guess this does not that deviated from flux way, though little confident. Is this a bad practice, or any alternatives?

The first action you described should be denoted as an Action Creator. This is something that is fired when an event handler is triggered. I think your question is asking how you should handle the data returned from the action creator?

In that case, you would write another Action receives that response and emits to the dispatcher. The dispatcher would then make it to the appropriate store, and that store would emit a change to all components listening. When the store emits, it will "refresh the whole component tree" (assuming you have store listeners and state listening to those changes).

This is just the flux way and allows you not to have a callback tree -- that would be bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM