Using your EventPropagator solves the communication problem, but does not handle data integrity. For example, if more than two components listen on the same event and use the payload of this event to update their own states. The more components listen to the same events, the more difficult it is to make sure that all these components have the same data in their own states.
Secondly, Flux / Redux provides a unidirectional data stream. A simple example would be about 2 components A and B consuming the same X data from an external source (API or storage). The user can interact with any of them to get the latest X data. Now, if the user asks B to update X, we have two solutions with your EventPropagator:
- B will update X itself and generate an event to inform A of the update, to allow A to re-render. The same goes for A if the user asks A to update X. This is a bi-directional data stream.
- B triggers an event for A requesting an update of X, and waits until A fires an event to receive an updated X. If the user requests an update of A , A will do it himself and report B. This is a unidirectional data stream.
When the number of components increases and communication is not limited to only A & B, you may need to stick to only one of these two solutions to prevent glitches in your application logic. Flux / Redux chooses the second, and we are pleased with it.
If you really don't think you need another library for data management, you should take a look at the latest React: Context feature . It provides the most important functions that a data management library can have, and this is only React. It is noted that you must upgrade your React project to 16.3 in order to use this feature.
blaz
source share