Created
October 11, 2019 15:38
-
-
Save johnfischelli/da1a4d839c551364fa5071d3395bc042 to your computer and use it in GitHub Desktop.
Adding a custom redux store to Flex WebChat UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}); | |
)} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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