Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React/Flux - Why do I need a action-dispatcher?

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?

like image 938
Felix Hagspiel Avatar asked Dec 10 '15 13:12

Felix Hagspiel


People also ask

What is the need of dispatcher in flux?

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.

What is the use of dispatcher in React?

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.

What is likely to happen in Reactjs flux when a dispatch is triggered and the store gets updated?

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.

Is Flux easier than Redux?

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.


1 Answers

The dispatcher fires actions one by one, when they are called. You need a dispatcher because:

  1. 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.

  2. 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!

  3. 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.

like image 62
Bhargav Ponnapalli Avatar answered Sep 25 '22 01:09

Bhargav Ponnapalli