Coder Social home page Coder Social logo

flux-redux-migration's Introduction

Gradually Migrating from Flux to Redux

This README is one way you could go about Migrating a large flux app to Redux

Editing a flux store

Consider a flux store, TodoStore

While you are migrating to redux, the first step would be to create the reducer for this store. However, remeber that it is important that you keep the API provided by your store the same. This is because your components are still dependent on the flux store's listeners and getter functions.

Thus one way to go about this, is to split your flux store into 2 files as follows

// FILE: intermediateFluxStore.js

//IMPORTS

//CHANGE EVENTS
var CHANGE_EVENT = 'change';

/**
 * Keep it in migration folder. You can delete the entire migration folder when you have created all
 * reducers.
 */
import {default as createFluxStore} from 'migration/createFluxStore.js';
import {todos} from 'reducers/todos.js';

/**
  * The createFluxStore is different from the original redux/createStore in 2 ways,
  * 1. It accepts both action.type and action.actionType. It also adds the 'type' key so that your reducer
  *    will work as required.
  *
  * 2. It passes the action['type'] as parameter to the subscribed callbacks. You can use this if you have 
  *    to emit different events based on the actionType.
  */

var todoReduxStore = createFluxStore(todos);
//You can replace this with createStore once your components become aware of redux through store.subscribe or react-redux.

var TodoStore = assign({}, EventEmitter.prototype, {
  getAll: function() {
    //Now we take data from the store.
    return todoReduxStore.getState()['_todos'];
  },

  emitChange: function() {
    this.emit(CHANGE_EVENT);
  },
  addChangeListener: function(callback) {
    this.on(CHANGE_EVENT, callback);
  },
  removeChangeListener: function(callback) {
    this.removeListener(CHANGE_EVENT, callback);
  },

  dispatcherIndex: AppDispatcher.register(function(payload) {
    var action = payload.action;
    //If you are using createFluxStore
    todoReduxStore.dispatch(action);
    //EndIf
     
    //If you are using createStore from Redux
    action['type'] = action.actionType;
    todoReduxStore.dispatch(action);
    //EndIf
  })

});

todoReduxStore.subscribe(function(actionType){
  /**
  * Here you can emitChange based on the actionType.
  * 
  */
})

Next the reducer file,

//FILE: reducer.js

//REQUIRED IMPORTS
const initialState = {
  '_todos':{}
}

export function todos(state=initialState, action){
  // Remember your actions must have type key. createFluxStore adds actionType to the type internally.
  switch(action.type){ 
  
  
    //Here you will add the logic which was present in your store's dispatch Handler.
  
    default:
     return state;
  }
}

With this approach,

  1. None of your components will break since the store's listeners and getters are still present

  2. Getter functions are retrieving data from your redux store. Thus, Tests you have return for your flux store will work here.

  3. This Intermediate store can exist alongside flux stores, until you migrate them to redux stores.

  4. Finally, you could simply combine all the reducers and create a single store.

flux-redux-migration's People

Contributors

vivek3003 avatar

Watchers

James Cloos avatar Makoto Tateno avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.