Skip to content

Instantly share code, notes, and snippets.

@meftunca
Created March 19, 2020 12:16
Show Gist options
  • Save meftunca/f6b893bbe414a978af6566e8c2f64154 to your computer and use it in GitHub Desktop.
Save meftunca/f6b893bbe414a978af6566e8c2f64154 to your computer and use it in GitHub Desktop.
React native paper snackbar (without context api)
import React, {useEffect, useState} from 'react';
import {Snackbar} from 'react-native-paper';
const GlobalSnackbar = () => {
const [state, setBaseState] = useState({
visible: false,
action: null,
message: '',
duration: 3000,
style: null,
});
const setState = newState => setBaseState(Object.assign({}, state, newState));
useEffect(() => {
global.snackbar = ({message, type = 'default', style, ...otherProps}) => {
setState({
visible: true,
message,
style: [snackbarStyles[type], style || state.style],
...otherProps,
});
};
}, []);
return (
<Snackbar
onDismiss={() => setState({visible: false})}
visible={state.visible}
duration={state.duration}
style={state.style}
action={state.action}>
{state.message}
</Snackbar>
);
};
const snackbarStyles = StyleSheet.create({
default: {},
success: {
backgroundColor: '#10AB2C',
},
error: {
backgroundColor: '#FF5733',
},
info: {
backgroundColor: '#00ADBB',
},
primary: {
backgroundColor: '#1869FF',
},
});
export default GlobalSnackbar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment