Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flux and ReactJS

Flux and ReactJS

An introduction to Flux and how to integrate ReactJS into it.

Avatar for Rei Colina

Rei Colina

January 26, 2016

Other Decks in Programming

Transcript

  1. ?

  2. - What is it? - Anatomy - Action - Dispatcher

    - Store - View - The Big Picture - Libraries - Demo - Pros & Cons
  3. Action Dispatcher Store View { actionType: ‘create’, data: data }

    function(data) { appDispatcher.dispatch( ); }
  4. Action Dispatcher Store View var TodoActions = { create: function(text)

    { AppDispatcher.dispatch({ actionType: TodoConstants.TODO_CREATE, text: text }); }, updateText: function(id, text) { AppDispatcher.dispatch({ actionType: TodoConstants.TODO_UPDATE_TEXT, id: id, text: text }); } };
  5. - App state (private vars) - No setters; only getters

    - Callbacks - Emits events on state change Action Dispatcher Store View
  6. Action Dispatcher Store View var TodoStore = assign({}, EventEmitter.prototype, {

    getAll: function() { return _todos; }, emitChange: function() { this.emit(CHANGE_EVENT); }, addChangeListener: function(callback) { this.on(CHANGE_EVENT, callback); }, removeChangeListener: function(callback) { this.removeListener(CHANGE_EVENT, callback); } });
  7. Action Dispatcher Store View AppDispatcher.register(function(action) { switch(action.actionType) { case TodoConstants.TODO_CREATE:

    create(action.text); TodoStore.emitChange(); break; case TodoConstants.TODO_UPDATE_TEXT: update(action.id, {text: action.text}); TodoStore.emitChange(); break; default: // no op } });
  8. Action Dispatcher Store View var CHANGE_EVENT = 'change'; var _todos

    = {}; function create(text) { ... } function update(id, updates) { ... } var TodoStore = assign({}, EventEmitter.prototype, { getAll: function() { return _todos; }, emitChange: function() { this.emit(CHANGE_EVENT); }, addChangeListener: function(callback) { this.on(CHANGE_EVENT, callback); }, removeChangeListener: function(callback) { this.removeListener(CHANGE_EVENT, callback); } }); AppDispatcher.register(function(action) { switch(action.actionType) { case TodoConstants.TODO_CREATE: create(action.text); TodoStore.emitChange(); break; case TodoConstants.TODO_COMPLETE: update(action.id, {complete: true}); break; case TodoConstants.TODO_UPDATE_TEXT: update(action.id, {text: action.text}); TodoStore.emitChange(); break; default: // no op } }); module.exports = TodoStore;
  9. Action Dispatcher Store View componentDidMount: function() { // add an

    event listener TodoStore.addChangeListener(this._onChange); }, componentWillUnmount: function() { // remove the event listener TodoStore.removeChangeListener(this._onChange); }
  10. Action Dispatcher Store View getInitialState: function() { // get data

    from the store return { allTodos: TodoStore.getAll() }; }
  11. Action Dispatcher Store View _onSave: function(text) { // change data

    in the store TodoActions.updateText(id, text); }
  12. Action Dispatcher Store View var TodoApp = React.createClass({ _onChange: function()

    { this.setState({ allTodos: TodoStore.getAll() }); }, _onSave: function(text) { TodoActions.updateText(id, text); } getInitialState: function() { return getTodoState(); }, componentDidMount: function() { TodoStore.addChangeListener(this._onChange); }, componentWillUnmount: function() { TodoStore.removeChangeListener(this._onChange); }, render: function() { return (<div> ... </div>); }, }); module.exports = TodoApp;
  13. AJAX API Calls var _todos = {}; AppDispatcher.register(function(action) { switch(action.actionType)

    { case Constants.FETCH_TODOS: $.ajax({ url: '/todos/', success: function (data) { // update _todos } }); break; } }); From Stores Action Dispatcher Store View
  14. AJAX API Calls var TodoActions = { fetchTodos: function(text) {

    $.ajax({ url: '/todos/', success: function (data) { // dispatch an action } }); } }; From Actions Action Dispatcher Store View
  15. - Data is easy to follow - Stores as source

    of truth - Testable - Boilerplate code - Easy to fall into anti-patterns