I understand that I need a emit.change()
dispatcher, to let all components know that something changed inside the store. But I dont understand why I need to dispatch actions rather than calling stores directly from inside the actions,
.i.e. why should I do this:
var Dispatcher = require('dispatcher');
var MyActions = {
addItem: function(item){
Dispatcher.dispatch({
action: 'ADD_ITEM',
payload: item
})
}
}
rather than this:
var MyStore = require('mystore');
var MyActions = {
addItem: function(item){
MyStore.addItem(item);
}
}
Is that for the case that multiple stores listen to the same event, for example when StoreA
and StoreB
listen to ADD_ITEM
as well?
Dispatcher is used to broadcast payloads to registered callbacks. This is different from generic pub-sub systems in two ways: Callbacks are not subscribed to particular events. Every payload is dispatched to every registered callback.
Introduction to React Dispatcher. The dispatcher is the center point of the data flow in a Flux application. It is simply a directory of call-backs and incites these callbacks in a particular order. Callbacks are stored in each and every store with a dispatcher.
Stores get updated because they have a callback that is registered using the dispatcher. Node's event emitter is used to update the store and broadcast the update to view. The view never directly updates the application state. It is updated because of the changes to the store.
When comparing the usability of Flux vs Redux, both score the same. But Redux is not just a state management library, it offers several benefits for your front-end apps, including ensuring data consistency, sharing data between components and providing templates for code organization.
The dispatcher fires actions one by one, when they are called. You need a dispatcher because:
You want the application state to be changed atomically. Which means, s1->s2(a1), s2->s3(a2) in a synchronous manner. Rather than s1->s3 (because of a1 and a2). If you don't do it, you will have to worry about other actions firing along with this particular action and guess how the application state will change for all those combinations. This is where all hell breaks loose and your code will become too difficult to maintain. Imagine writing an if-else block in the store for each action fired, to check if other actions are also active . The dispatcher makes sure that it does not dispatch while already dispatching
. One dispatch at a time. Keeps your state tree very healthy.
Also the dispatcher maintains an array of callbacks to fire for each 'action'. This is useful for calling callbacks on multiple stores for the same action. When a store subscribes to an action (using register
), the dispatcher adds the registerHandler associated with it and adds it to an array. With the help of this, you can register/unregister your stores when you need them. And depending on the action type, you can make changes accordingly to all the stores registered. If you don't use a dispatcher, you will have to worry about all the stores which have to notified, when you are writing the action part. Bad!
With this kind of approach, the only thing you have to focus on, is hitting the dispatcher with an action. The rest is upto the dispatcher which notifies all the stores which need to change
based on the action. Since the stores have callbacks which trigger the views, those callbacks can be called as and when needed. This keeps your code very modular.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With