I'm using React with Redux and Material UI to build a webapp. The webapp is made of several pages and components. I know that a snackbar or dialog should be directly connected to what the user is doing. However, I'd like to make the snackbar and dialog independent on the pages and components. A use case therefore is displaying a message like background synchronization of your data failed
and an action retry now
. My idea was to render the snackbar on a page called RootFrame
, which is used to wrap all other pages and to dispatch the text of the snackbar as payload of an action.
My Redux action to show a snackbar:
export function showSnackbar(message: string) {
return {
type: SHOW_SNACKBAR,
payload: message
};
}
Of course it might also be good to specify the message in the action instead of taking the message as argument but that's not my problem right now. The problem is: How can I use this system and display a snackbar with an action? Can I change my action to
export function showSnackbar(message, action) {
return {
type: SHOW_SNACKBAR,
payload: {
message,
action
}
};
}
and render my snackbar in the RootFrame
like
<Snackbar
message={this.props.message}
ref='snackbar'
onDismiss={() => this.props.dispatch(dismissSnackbar())}
action='retry now'
onActionTouchTap={() => this.props.dispatch(this.props.action)}
/>;
When the snackbar is dismissed, an action changes a variable in the state: snackbar.visible = false
. This is used to activate the snackbar (it is rendered when snackbar.visible === true
). When the user clicks on retry now
, the action to start the sync (which is passed to the component as props) should be dispatched. The problem is very similar when using dialogs. So not only the text to display but also the next possible actions have to be passed to the component.
Do you think using Redux like this is ok or is there a better solution?