In this blog post, we are going to go through the implementation of the Redux integration on the SUSI.AI web clients. The existing SUSI.AI WebChat codebase has Flux integrated into it, but integrating Redux would make it a lot easier to manage the app state in a single store. And would result in a more maintainable and performant application. Let us go through the implementation in the blog –
The key steps involved the following –
- Restructuring the directory structure of the repository to enable better maintenance.
- Creating a Redux store and configuring the middlewares.
- Standardizing the format for writing actions and make API calls on dispatching an action.
- Standardizing the format for writing reducers.
- Hook the components to the Redux store.
Restructuring the directory structure
- All the redux related files and utils are put into the redux directory, to avoid any sort of confusion, better maintenance and enhanced discoverability. The prime reason for it also because the integration was done side-by-side the existing Flux implementation.
- The actions and reducers directory each has a index.js, which exports all the actions and reducers respectively, so as to maintain a single import path for the components and this also helped to easily split out different types of actions/reducers.
Creating Redux store and configure middlewares
- The function createStore takes in the browserHistory (provided by React Router) and returns a single store object that is passed on to the entry point component of the App.
- The store is passed to the application via the <Provider> component, provided by the react-redux. It is wrapped to the <App> component as follows –
- The 2 middlewares used are routerMiddleware provided by the react-router-redux and the reduxPromise provided by redux-promise.
- The routerMiddleware enhances a history instance to allow it to synchronize any changes it receives into application state.
- The reduxPromise returns a promise to the caller so that it can wait for the operation to finish before continuing. This is useful to assist the application to tackle async behaviour.
Standardizing the actions and making API calls on action dispatch
- The actions file contains the following –
- As new actions are added, it can be added to the actionTypes file and can be added in the export statement of the above snippet. This enables very standard and easy to manage actions.
- This approach allows to handle both types of action dispatch – with and without API call. In case of API call on dispatch, the action resolves with the payload of the API.
- The APIs are called via the AJAX helper (Check out this blog – https://blog.fossasia.org/make-a-helper-for-ajax-requests-using-axios/ ).
Standardizing the reducers and combining them
- The reducers file contains the following –
- The default application state is defined and the reducer corresponding to each action type returns an immutable object which is the new application state,
- In the above example, the payload from the getApiKeys API call is received in the reducer, which then updated the store.
- The reducers/index.js combines all the reducers using combineReducers provided by redux and exports a single object of reducers.
Hook the components to the Redux store
- After Redux integration and standardization of the reducers, actions, any of the component can be hooked to the store as follows –
- The mapStateToProps is a function that is used to provide the store data to the component via props, whereas mapDispatchToProps is used to provide the action creators as props to the component.
The above was the implementation of Redux in the SUSI.AI Web Clients. I hope the blog provided a detailed insight of how Redux was integrated and the standards that were followed.