React Tutorials - Day Six - Flux

This is the sixth part of this series. The list of previous articles is given below.

Today, we will learn about Flux -  what is Flux, why we need the Flux concept, how to setup Flux development environment and more about the fFux concept. Let’s start today’s session.

What is Flux?

Flux is a programming architecture and concept where data is flown in a unidirectional way. Flux is not a framework or library, it is just an architecture pattern that was developed by Facebook along with the React View library to manage the data flow into our application. Flux makes it easier to track the changes and bugs during development.

React

The four pillars of Flux are  - Action, Dispatchers, Store, and Component.

  • Actions - Actions are sent to dispatchers to start the data flow.
  • Dispatcher - Dispatchers are like pub hub; all the data is dispatched to registered callbacks.
  • Store - Container for the state and logic; Every store contains particular state and updates it when any change occurs.
  • Component - Components receive the data from the stores.

Why do we need Flux ?

Before we start working on any new concept and technology, first of all, we should think why we need this new concept or technology; what type of the drawbacks it is going to remove. Now, let's consider all the above points for Flux and find out the actual requirement of Flux concept in React.

Point 1 - Where the data comes from.

In React applications, data in a component can come from its own property or from the parent component and from the states of the component. So, there can be multiple data sources for a single component that make it difficult to track the data source. Now, Flux is used here to create a single storage of the data.

Point 2 - How to distribute data with other components.

Suppose you are working on a large application where the number of components is hundreds and you want to share the data of one component with another component. So what will you do?

For such scenarios, first of all we transfer this data to the root component and then distribute this data to other components. Then, what if a large number of components want to share their data? In that situation, if we use the same approach, then it makes our application more complex and slows down the performance. But we can use Flux here. Using Flux, we move all the changes to stores and rest of the components can access this data from the stores.

Point 3 - Compare with MVC architecture.

To best describe Flux, we will compare it to one of the most popular client-side architectures -  MVC (Model view Controller) .

React

Let’s take a simple example of MVC pattern. In MVC, each View reads the data from Model. When we make any change in Model through View, it will trigger some events in Controller and now the Controller knows how to coordinate these changes of these Models by calling the methods of Model. After making all the changes in Model, it will notify the View to read the new data from Models and re-render the changes. This approach is useful in small scale applications. If we use large amounts of data, then we need another apporach. Let’s take an example where the number of Models, Controllers, and Views is very large.

React

Now we have added 4 new Models and 4 new Views in previous example. You can see that the dependency becomes more complex. When any user interacts with View and updates any data, there are multiple branches associated to a View and for each branch, it will update the corresponding Model and it may be possible that Model will trigger updates in another Models and after the making the changes into Model, it will reflect on Views. In such kind of scenario, it becomes extremely difficult to find out which branch contains bugs in our application and it becomes hard to debug the cascading changes. The final result is an error prone application.

One another problem is that some times our application contain infinite cycle means If you look the above diagram can you tell that there is not any cycle which means where Model changes the View and View again Changes to Models because of a cycle. The main reason behind of all these issues is Flow of the data. In MVC data does not flow in a one way direction. We can solve all the issues by making the data flow in a certain direction.

Now Flux is comes here with a new approach that is one way data flow.

React

All the users interact with View and View get its data from Store. Store is the just like a container that contains some particular state and logic . When any user makes the changes into View it will trigger a call method to the dispatcher and now the dispatcher makes these changes into particular Stored data and as the Store updates itself it will send the new data to all Views and View re-renders all these changes. Here dispatcher is just like a traffic controller and controls the data flow into our application and Stores are just like the data layer of our application.

React

Now we take an example where the number of Store and View is multiple. You can see that data flow is not changed after adding the multiple Views and Store into Application. Whenever a view sends any changes to dispatcher then dispatcher sends these changes to all Stores and it never tells the Store how to makes the updates it only send the changes to Store . Store contains all the business logic for a specific domains and updates only when the data is corresponding their domain and after making all changes it triggers the changes to all Views that are associated with Store and View can re render the all new changes.

Now we can understand that data is flow in one direction only there is not any two way arrow just like MVC pattern. So if any error or bug is occur in application then you don’t need to go through the multiple paths to find out the incorrect logic. I think all above examples are enough to describe how Flux helps React to make a robust and scalable application. Now we will focus on the four pillars of the Flux pattern and read about the each one by one.

Dispatcher

The Dispatcher is like a pub sub that work like a traffic controller and broadcasts the payloads to all the registered callbacks. Dispatcher takes the Actions and dispatches the data to all callbacks. Callbacks are not subscribed to a specific events when new data icomes then dispatcher sends the data to all registered callbacks by using the dispatch method.

Following are the APIs associated with Dispatcher

  • Register(function callback) - Register a callback that's invoked with each dispatch data
  • Dispatch(object) - Dispatch payloads to all registered callbacks
  • isDispatching - Tells if the Dispatcher currently dispatching or not
  • unregister(string id) - Unregister a callback
  • waitFor(array<string>) - The store waits for the completion of other callbacks before continuing the callback.

    React

    In the above image we created a simple dispatcher and export this dispatcher.

    React

    In this image we import the dispatcher into action and use the “dispatch” API of dispatcher and send some payload(data) to all the registered callbacks.

Stores

Store in Flux are just like a data wareshouse that holds all the data for applications and provides this data to all components. In Flux pattern we can have multiple stores and each store contains some specific data.

Let’s take an example of store.

  1. import {EventEmitter} from "events";  
  2.   
  3. import  Dispatcher from "../dispatcher/dispatcher";  
  4. class TodoStore extends EventEmitter{  
  5.   
  6. constructor(){  
  7.     super();   
  8.     this.todoList=[  
  9.         {  
  10.             id:110,  
  11.             text:"Take your breakfast",  
  12.             priority:"High"  
  13.         },  
  14.          {  
  15.             id:111,  
  16.             text:"Meet Rahul Prajapat",  
  17.             priority:"High"  
  18.         },  
  19.         {  
  20.             id:112,  
  21.             text:"Read a book",  
  22.             priority:"Low"  
  23.         },  
  24.         {  
  25.             id:113,  
  26.             text:"Organize your desk",  
  27.             priority:"Medium"  
  28.         }  
  29.     ]  
  30. }  
  31.   
  32. getAll(){  
  33.     return this.todoList;  
  34. }  
  35.   
  36. addNewItem(task,priority){  
  37.   
  38. var newid=100+this.todoList.length;  
  39.   
  40. this.todoList.push({  
  41.     id:newid,  
  42.     text:task,  
  43.     priority:priority  
  44. });  
  45. this.emit('change');  
  46.   
  47. }  
  48. removeItem(id){  
  49.   
  50. this.todoList=this.todoList.filter(context=>context.id!=id);  
  51.   
  52. this.emit('change');  
  53. }  
  54. handleAction(action){  
  55.   
  56.    switch(action.type){  
  57.        case "create_new":{  
  58.            this.addNewItem(action.task,action.priority);  
  59.        }  
  60.        case "delete":{  
  61.            this.removeItem(action.id);  
  62.        }  
  63.    }   
  64. }  
  65.   
  66. }  
  67.   
  68. const todoObj=new TodoStore();  
  69. Dispatcher.register(todoObj.handleAction.bind(todoObj));  
  70.   
  71. export default todoObj;  

In the above code we insert some initial data into store. In each store there are three important thing that are following .

  1. Each store must be use the “EventEmitter” class of Node.js because using this eventemitter class we emit the events that are listened to  by our component and the components get the updates using these events.

    React

  1. If we want to get new payloads from dispatcher then we have to register a callback for the dispatcher in our store as below

    React

  1. After making the all required changes into Store we have to emit the events so that all the components can listen to  these events and get the new updates.

    React

Components(Views)

In components we listen for some specific events that are emitted from the stores. Whenever we get any events that means some data into the store has been updated and it gets the new data from the store and updates the states of our components.

React

Actions

Actions are just collections of methods that are called from the Views and each method dispatches a dispatcher with some data.

React

So whenever we invoke any method of an action from view actually it dispatches the dispatcher. Now all the stores that are registered to this dispatcher invoke the callback function and update the store data and emit an event for the views. Now all the components(views) that are listing this event can get the new data from the stores and re render the views.

After getting enough theoretical knowledge about the Flux now we'll do something practical with the Flux. Compared to previous articles in this series, I made a lot of changes in the  application like using the bootstrap . So first of all update the previous pages as below.

Index.html

  1. <!DOCTYPE html>  
  2. <html lang = "en">  
  3.   
  4.    <head>  
  5.       <meta charset = "UTF-8">  
  6.       <title>React App</title>  
  7.       <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">  
  8.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  
  9.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  
  10.    </head>  
  11.   
  12.    <body>  
  13.       <div  id = "app"></div>  
  14.       <script src = "/app/bundle.js"></script>  
  15.    </body>  
  16.   
  17. </html>  

Header.jsx

  1. import React from 'react';  
  2. import { Link } from 'react-router-dom'  
  3.   
  4. class Header extends React.Component {  
  5.     
  6.   constructor(){  
  7.      super();  
  8.   }  
  9.     
  10.     
  11.    render() {  
  12.       
  13.       return (  
  14.         <div>  
  15.           <nav className="navbar navbar-default">  
  16.   <div className="container-fluid">  
  17.     <div className="navbar-header">  
  18.       <Link className="navbar-brand"  to='/' >Home</Link>  
  19.     </div>  
  20.     <ul className="nav navbar-nav">  
  21.       <li className="active"><Link to='/' >Home</Link></li>  
  22.       <li><Link to='/details' >Details</Link></li>  
  23.       <li><Link to='/aboutus'>AboutUs</Link></li>  
  24.        
  25.     </ul>  
  26.   </div>  
  27. </nav>  
  28.     
  29.           
  30.          </div>  
  31.       );  
  32.    }  
  33. }  
  34.   
  35. export default Header;  

home.jsx

  1. import React from 'react';  
  2.   
  3. class Home extends React.Component {  
  4.   constructor(){  
  5.      super();  
  6.   }   
  7.    render() {      
  8.       return (  
  9.         <div>  
  10.          <div className="alert alert-success alert-dismissible fade in" role="alert">  
  11.     <button type="button" className="close" data-dismiss="alert" aria-label="Close">  
  12.       <span aria-hidden="true">×</span></button>  
  13.     <strong>Welcome  !  </strong>  
  14.   This is Home Page of Application  
  15.     
  16.   </div>  
  17.          </div>  
  18.       );  
  19.    }  
  20. }  
  21.   
  22. export default Home;  

Main.jsx

  1. import React from 'react';  
  2. import Footer from '../footer/footer';  
  3. import Header from '../header/header';  
  4. import  Details from '../details/details';  
  5. import  Home from '../home/home';  
  6. import  AboutUs from '../aboutus/aboutus';  
  7. import { Switch, Route } from 'react-router-dom'  
  8.   
  9. class Main extends React.Component {  
  10.   constructor(){  
  11.      super();  
  12.      this.age=12000;   
  13.    }  
  14.    render() {  
  15.           return (  
  16.           <div>  
  17.            <Header></Header>  
  18.        <div className="container">  
  19.       <Route exact path='/' component={Home}/>  
  20.       <Route exact path='/details' component={Details}/>  
  21.       <Route exact path='/aboutus' component={AboutUs}/>  
  22.       </div>  
  23.       </div>  
  24.       );  
  25.    }  
  26. }  
  27.   
  28. export default Main;  

After making all the above changes now the following will be the screen of our application.

React
Create Dispatcher

Now create a folder in “src” directory and named this folder as “dispatcher” . After creating the folder now create a “dispatcher.js” file and paste the following code into this file.

Dispatcher.js

  1. import { Dispatcher} from "flux";  
  2. export default new Dispatcher;  

In above code we create and export a dispatcher object, now we use this dispatcher into our store and actions.

Create Store

Now we create another folder into “src” directory and named this folder as “stores” in this folder we will create our store classes. Now create a “todoStore.js” file into this folder and paste the following code into this file.

todoStore.js

  1. import {EventEmitter} from "events";  
  2. import  Dispatcher from "../dispatcher/dispatcher";  
  3. class TodoStore extends EventEmitter{  
  4.   
  5. constructor(){  
  6.     super();   
  7.     this.todoList=[  
  8.         {  
  9.             id:110,  
  10.             text:"Take your breakfast",  
  11.             priority:"High"  
  12.         },  
  13.          {  
  14.             id:111,  
  15.             text:"Meet Rahul Prajapat",  
  16.             priority:"High"  
  17.         },  
  18.         {  
  19.             id:112,  
  20.             text:"Read a book",  
  21.             priority:"Low"  
  22.         },  
  23.         {  
  24.             id:113,  
  25.             text:"Organize your desk",  
  26.             priority:"Medium"  
  27.         }  
  28.     ]  
  29. }  
  30.   
  31. getAll(){  
  32.     return this.todoList;  
  33. }  
  34.   
  35. addNewItem(task,priority){  
  36.   
  37. var newid=100+this.todoList.length;  
  38.   
  39. this.todoList.push({  
  40.     id:newid,  
  41.     text:task,  
  42.     priority:priority  
  43. });  
  44. this.emit('change');  
  45.   
  46. }  
  47. removeItem(id){  
  48.   
  49. this.todoList=this.todoList.filter(context=>context.id!=id);  
  50.   
  51. this.emit('change');  
  52. }  
  53. handleAction(action){  
  54.   
  55.    switch(action.type){  
  56.        case "create_new":{  
  57.            this.addNewItem(action.task,action.priority);  
  58.        }  
  59.        case "delete":{  
  60.            this.removeItem(action.id);  
  61.        }  
  62.    }   
  63. }  
  64.   
  65. }  
  66.   
  67. const todoObj=new TodoStore();  
  68. Dispatcher.register(todoObj.handleAction.bind(todoObj));  
  69. export default todoObj;  

In above code we import the Dispatcher that we created earlier and add some raw data in “todoList” array.

React

A point to notice is  that we import the “EventEmitter” class of the Node.js. We import this class because after making any changes into store we need to emit an event that can be listened to by the View(component) and re render the view.

React

In the above lines of code we register a callback function to “Dispatcher” object and in this callback function as per values of “type” property we are calling two different methods one for adding a new item into “todoList” and another for removing a particular item from the list.

React

In both methods after making any changes at last we emit a “Change” event, so views that are listening  to this event can get the new data from store and make the required changes into view.

React

We also create a “getAll” method into store that return the “todoList” data, so using this method we will get the data from this store into our view(component).

Action

Create a “action” folder into “src” directory and after creating this folder now create a “action.js” file and paste the following code into this file.

  1. import Dispatcher from "../dispatcher/dispatcher";  
  2.   
  3. export function createToDo(task,priority){  
  4.     Dispatcher.dispatch({  
  5.         type:"create_new",  
  6.         task:task,  
  7.         priority:priority  
  8.     });  
  9. };  
  10.   
  11. export function deleteToDo(id){  
  12.     Dispatcher.dispatch({  
  13.         type:"delete",  
  14.         id:id  
  15.     });  
  16. };  

In above action.js file we create two different methods one for adding a new item into store and another for removing a particular item from the store object. Both method use the “dispatch” API of the Dispatcher that dispatch payloads to registered callback. Now we use these actions methods into our views section and perform add and delete operations.

Details Component

Now open “details.jsx” file and paste the following code into this file.

details.jsx

  1. import React from 'react';  
  2. import { Router } from 'react-router';  
  3. import ReactDOM from 'react-dom';  
  4. import todoObj  from '../stores/todoStore';  
  5. import * as todoActions from "../action/action"  
  6. class Details extends React.Component {  
  7.   constructor(props){  
  8.      super();  
  9.   this.state={  
  10.     todos:todoObj.getAll()  
  11.   }    
  12. }  
  13.   
  14. addTodo(){  
  15. todoActions.createToDo(ReactDOM.findDOMNode(this.refs.taskName).value,  
  16.     ReactDOM.findDOMNode(this.refs.priority).value);  
  17. }  
  18.   
  19. renoveToDo(id){  
  20.  todoActions.deleteToDo(id);  
  21. }  
  22.   
  23. componentWillMount() {  
  24.   todoObj.on("change",()=>{  
  25.     this.setState({  
  26.       todos:todoObj.getAll()  
  27.     })  
  28.   })  
  29. }   
  30.     
  31.   
  32.    render() {      
  33.       return (  
  34.         <div>  
  35.            <table id="mytable" className="table table-bordred table-striped">  
  36.                      
  37.                    <thead><tr>  
  38.                   <th>Task</th>  
  39.                   <th>Priority</th>  
  40.                   <th>Delete</th>  
  41.                   </tr>  
  42.                    </thead>  
  43.                   <tbody>  
  44.                     {this.state.todos.map((data, i) => (  
  45.             <tr key={i}>  
  46.                <td>{data.text}</td>  
  47.     <td>{data.priority}</td>     
  48.     <td><p data-placement="top" data-toggle="tooltip" onClick={this.removeToDo.bind(this,data.id)}  
  49.      title="Delete"><button className="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span className="glyphicon glyphicon-trash"></span></button></p></td>  
  50.         </tr>  
  51.           ))}  
  52.         
  53.     </tbody>  
  54.           
  55. </table>  
  56.     
  57.       <div className="col-md-6 col-sm-12 col-md-offset-3">  
  58.         <div >  
  59.     <div className="form-group">  
  60.       <label htmlFor="Task">Task:</label>  
  61.       <input type="Task" className="form-control" ref="taskName" id="Task" placeholder="Enter Task" name="Task"/>  
  62.     </div>  
  63.     <div className="form-group">  
  64.       <label htmlFor="Priority">Priority:</label>  
  65.       <input type="Priority" className="form-control" ref="priority" id="pwd" placeholder="Enter Priority" name="Priority"/>  
  66.     </div>  
  67.     <button type="submit" onClick={this.addTodo.bind(this)} className="btn btn-success">Submit</button>  
  68.   </div>  
  69.         {/*<input type="text" ref = "myInput"/><br/> 
  70.  
  71.         <input type="button" value="Add ToDo" onClick={this.addTodo.bind(this)} />*/}  
  72.         </div>  
  73.          </div>  
  74.       );  
  75.    }  
  76. }  
  77.   
  78.   
  79. export default Details;  

After making all the changes now save these changes and refresh the browser. When you click on “Details” menu option you will get the following screen.

React

In constructor function we call the “getAll” method of the “todoStore” and insert the initial data into the state of our components and render the data into table format with delete and edit option. We will consider these methods later first of all we  will learn how to add new item into “todoList”.

React

Add new item into todo List

React

To add a new item into todo list we create two textboxes and add ref keyword to each textbox, actually ref keyword in React is used for get the reference of any element from the “HTML DOM” and onClick method we call the “addTodo” method.

React

In this method we get the data from both textboxes and pass the values of both textboxes into “createToDo” method of the action that we created earlier. Let’s try to add some new data into “todoList”.

React

React

After adding the new item now we will delete some record from this “todolist” for this we add a delete button in each item of the list. When we click on this button it will delete that particular item from the list.

React

React

For deleting a particular item we call the “removeToDo” method on click event of the button and pass the id of the item.

React

In “removeToDo” method we call the “deleteToDo” method of the Action and pass the id of item to delete.

React

You can see that whenever we add or delete the item, the list in our view re-renders that update immediately. For this, we need to add the callback function for the “change” event into “componentWillMount” phase of our component. As we discussed earlier whenever we make any change into our store, we emit an “onchange” event, so all the components that are listening to this event can get new fresh data from the store and update the View.

React

One of the major advantages of the Flux pattern is that if multiple components are registered to a store and if any component makes any changes into store data, these changes will also reflect on other components and all components will remain synchronized with the store data. Let’s take an example. Open your “aboutus.jsx” file and paste the following code into this file.

aboutus.jsx

  1. import React from 'react';  
  2. import todoObj  from '../stores/todoStore';  
  3. import * as todoActions from "../action/action"  
  4. class AboutUs extends React.Component {  
  5.   constructor(props){  
  6.      super();  
  7.    this.state={  
  8.     todos:todoObj.getAll()  
  9.   }    
  10. }  
  11.   
  12. componentWillMount() {  
  13.   todoObj.on("change",()=>{  
  14.     this.setState({  
  15.       todos:todoObj.getAll()  
  16.     })  
  17.   })  
  18. }  
  19.   
  20.    render() {      
  21.       return (  
  22.         <div>  
  23.       <table id="mytable" className="table table-bordred table-striped">  
  24.                      
  25.                    <thead><tr>  
  26.                   <th>Task</th>  
  27.                   <th>Priority</th>  
  28.                    
  29.                   </tr>  
  30.                    </thead>  
  31.                   <tbody>  
  32.                     {this.state.todos.map((data, i) => (  
  33.             <tr key={i}>  
  34.                <td>{data.text}</td>  
  35.     <td>{data.priority}</td>     
  36.       
  37.         </tr>  
  38.           ))}  
  39.         
  40.     </tbody>  
  41.           
  42. </table>  
  43.          </div>  
  44.       );  
  45.    }  
  46. }  
  47.   
  48. export default AboutUs;  

In aboutus.jsx file, we are displaying simple Todo list data.

React

So now, if you make any changes into store, like add a new item into list from “details” page,

React

And after making these changes, now if you open the “aboutus” page, then you will get the same data as “details” page.

React

So, Flux is better approach that provides a central storage of data, and all its components always remain synchronized with any type of the changes into data.

Conclusion

In this article, we learned about Flux pattern. If you are working with a React application, you can also develop applications without Flux approach but in that case you don’t have any clear view about the data flow at certain points in your application. Flux provides a unidirectional flow of data and makes it easy to track the data changes and find out the bug in the applications. If you have any questions, please write in the comment section.

You can download the latest source code of this project from GitHub. Given below is the link of repository.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now