if you have bigger app, event system is a lot better solution then passing props.
Think as flux recommends. component -> action -> dispatcher -> store
In store you would have your state. You would register callbacks of components to store. You fire action from any component and any other component, that is listening for store's changes are getting data. No matter how you change your hierarchy you always get you data where it's required.
dispatcher.js:
var Promise = require('es6-promise').Promise;
var assign = require('object-assign');
var _callbacks = [];
var _promises = [];
var Dispatcher = function () {
};
Dispatcher.prototype = assign({}, Dispatcher.prototype, {
/**
* Register a Store's callback so that it may be invoked by an action.
* @param {function} callback The callback to be registered.
* @return {number} The index of the callback within the _callbacks array.
*/
register: function (callback) {
_callbacks.push(callback);
return _callbacks.length - 1;
},
/**
* dispatch
* @param {object} payload The data from the action.
*/
dispatch: function (payload) {
var resolves = [];
var rejects = [];
_promises = _callbacks.map(function (_, i) {
return new Promise(function (resolve, reject) {
resolves[i] = resolve;
rejects[i] = reject;
});
});
_callbacks.forEach(function (callback, i) {
Promise.resolve(callback(payload)).then(function () {
resolves[i](payload);
}, function () {
rejects[i](new Error('#2gf243 Dispatcher callback unsuccessful'));
});
});
_promises = [];
}
});
module.exports = Dispatcher;
some store sample:
const AppDispatcher = require('./../dispatchers/AppDispatcher.js');
const EventEmitter = require('events').EventEmitter;
const AgentsConstants = require('./../constants/AgentsConstants.js');
const assign = require('object-assign');
const EVENT_SHOW_ADD_AGENT_FORM = 'EVENT_SHOW_ADD_AGENT_FORM';
const EVENT_SHOW_EDIT_AGENT_FORM = 'EVENT_SHOW_EDIT_AGENT_FORM';
const AgentsStore = assign({}, EventEmitter.prototype, {
emitShowAgentsAddForm: function (data) {
this.emit(EVENT_SHOW_ADD_AGENT_FORM, data);
},
addShowAgentsAddListener: function (cb) {
this.on(EVENT_SHOW_ADD_AGENT_FORM, cb);
},
removeShowAgentsAddListener: function (cb) {
this.removeListener(EVENT_SHOW_ADD_AGENT_FORM, cb);
}
});
AppDispatcher.register(function (action) {
switch (action.actionType) {
case AgentsConstants.AGENTS_SHOW_FORM_EDIT:
AgentsStore.emitShowAgentsEditForm(action.data);
break;
case AgentsConstants.AGENTS_SHOW_FORM_ADD:
AgentsStore.emitShowAgentsAddForm(action.data);
break;
}
});
module.exports = AgentsStore;
actions file:
var AppDispatcher = require('./../dispatchers/AppDispatcher.js');
var AgentsConstants = require('./../constants/AgentsConstants.js');
var AgentsActions = {
show_add_agent_form: function (data) {
AppDispatcher.dispatch({
actionType: AgentsConstants.AGENTS_SHOW_FORM_ADD,
data: data
});
},
show_edit_agent_form: function (data) {
AppDispatcher.dispatch({
actionType: AgentsConstants.AGENTS_SHOW_FORM_EDIT,
data: data
});
},
}
module.exports = AgentsActions;
in some component you are like:
...
componentDidMount: function () {
AgentsStore.addShowAgentsAddListener(this.handleChange);
},
componentWillUnmount: function () {
AgentsStore.removeShowAgentsAddListener(this.handleChange);
},
...
this code is kind of old but it works well and you can definitely get idea of how stuff work