Dispatcher


Let’s understand dispatcher in simple words:

  • The dispatcher is basically the central hub for your application.
  • The dispatcher receives actions(from UI) and dispatches these actions and data to registered callbacks(functions) in the store.

Note :

  • The dispatcher broadcasts the payload to ALL of its registered callbacks(methods) in the store.
  • There is only one dispatcher, and it acts as the central hub within your application.

Let’s create our own dispatcher,

1.To create a dispatcher we need to install flux.

How to install flux in your Application
it’s easy: npm install flux

2.Now create a dispatcher file that returns the singleton dispatcher object.

dispatcher.js

import { Dispatcher } from "flux";//import flux to create dispatcher object

export default new Dispatcher;//return dispatcher object to use in action & store.


Once we have created dispatcher, now we have 2 build-in functions which we can use.

  • dispatch() – 1.used with actions
    2.dispatchs the actions to store.
  • Register() – 1.used in store
    2.Registers the store method to a dispatcher.