Skip to content

Instantly share code, notes, and snippets.

@johnfischelli
Created October 11, 2019 15:38
Show Gist options
  • Save johnfischelli/da1a4d839c551364fa5071d3395bc042 to your computer and use it in GitHub Desktop.
Save johnfischelli/da1a4d839c551364fa5071d3395bc042 to your computer and use it in GitHub Desktop.
Adding a custom redux store to Flex WebChat UI
import React from 'react';
import * as FlexWebChat from '@twilio/flex-webchat-ui';
import FlextendedState from './FlextendedState';
import NewReducer from './NewReducer';
class App extends React.Component {
state = {};
constructor(props) {
super(props);
FlexWebChat.Manager.create(configuration).then(manager => {
FlextendedState.addReducer('newReducer', NewReducer);
manager.store.replaceReducer(FlextendedState.combinedReducers());
// example dispatch
manager.store.dispatch({
type: 'SET_NEW_VALUE',
payload: {
newValue: true
}
});
)}
}
}
import {combineReducers} from 'redux';
import { WebchatReducer } from '@twilio/flex-webchat-ui'
const rootReducer = (function () {
const reducers = {flex: WebchatReducer };
return {
addReducer: function (slice, reducer) {
reducers[slice] = reducer;
},
combinedReducers: function () {
return combineReducers(reducers);
}
};
})();
export default rootReducer;
const initialState = {
value: false
};
const NewReducer = (state = initialState, action) => {
switch (action.type) {
case 'SET_NEW_VALUE':
return {
...state,
value: action.payload.newValue
};
default:
return state;
}
};
export default NewReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment