Install notistack
masterInstall the notistack package using your preferred package manager.
npm install notistack
yarn add notistackrepository·master·Indexed 26 days ago
https://github.com/iamhosseindhv/notistackA React library for displaying highly customizable notification snackbars (toasts) that can be stacked, queued, and triggered via function calls. It provides a SnackbarProvider for application wrapping, a useSnackbar hook for functional components, and an enqueueSnackbar function for triggering notifications from outside the React component tree. Version 3.0.2 is standalone, while earlier versions maintain peer dependencies on Material-UI.
Install the notistack package using your preferred package manager.
npm install notistack
yarn add notistackThe withSnackbar Higher-order component (HOC) has been removed. To migrate, you have two options:
useSnackbar hook.enqueueSnackbar or closeSnackbar directly from notistack to use them within your component logic.// Before
import { withSnackbar } from 'notistack'
class MyButton extends React.Component {
render() {
const { enqueueSnackbar } = this.props
}
}
export default withSnackbar(MyButton)
// After (Option 2: Direct Imports)
import { enqueueSnackbar, closeSnackbar } from 'notistack'
class MyButton extends React.Component {
render() {
// Use enqueueSnackbar directly from the import
}
}
export default MyButtonIn v2, HTML attributes (like data-* attributes) applied to the Snackbar root component must now be passed through the SnackbarProps object instead of being passed directly to the provider or the enqueue function. This applies to both the SnackbarProvider configuration and individual enqueueSnackbar calls.
// Provider configuration
<SnackbarProvider
SnackbarProps={{
'data-test': 'test',
}}
>
// Individual snackbar call
enqueueSnackbar('message', {
SnackbarProps: {
'data-test': 'test',
},
})Depending on your project's Material-UI version, you may need to install a specific version of notistack to satisfy peer dependency requirements:
npm install notistack@2.0.8.npm install notistack@latest-mui-v4.You can explore a live implementation of Notistack integrated with Redux using the CodeSandbox demo. This example demonstrates how to trigger snackbar notifications within a Redux-managed application state.
https://codesandbox.io/s/github/iamhosseindhv/notistack/tree/master/examples/redux-exampleFor a more idiomatic React approach, use the useSnackbar hook. Note that the component calling the hook must be a child of the SnackbarProvider to access the context.
import { SnackbarProvider, useSnackbar } from 'notistack';
// wrap your app
<SnackbarProvider>
<App />
<MyButton />
</SnackbarProvider>
const MyButton = () => {
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
return <Button onClick={() => enqueueSnackbar('I love hooks')}>Show snackbar</Button>;
};To show notifications, wrap your application in a SnackbarProvider and use the enqueueSnackbar function. This approach is useful for triggering notifications from outside the React component tree or in simple setups.
import { SnackbarProvider, enqueueSnackbar } from 'notistack';
const App = () => {
return (
<div>
<SnackbarProvider />
<button onClick={() => enqueueSnackbar('That was easy!')}>Show snackbar</button>
</div>
);
};You can explore the integration of Notistack with MobX by viewing the live demo on CodeSandbox.
https://codesandbox.io/s/github/iamhosseindhv/notistack/tree/master/examples/mobx-exampleTo create entirely customized snackbars or new variants, use the Components prop on the SnackbarProvider. Your custom component will receive all standard Notistack props (like variant and message) as well as any additional custom options passed via enqueueSnackbar.
<SnackbarProvider
Components={{
success: MyCustomSuccessNotification,
reportComplete: ReportComplete,
}}
>
</SnackbarProvider>
interface ReportCompleteProps extends CustomContentProps {
allowDownload: boolean;
}
const ReportComplete = React.forwardRef((props: ReportCompleteProps, ref) => {
const {
variant,
message,
allowDownload, // Custom prop passed via enqueueSnackbar
} = props;
// ...
});
// Triggering the custom variant with custom props
enqueueSnackbar('Your report is ready to download', {
variant: 'reportComplete',
persist: true,
allowDownload: true,
})When calling enqueueSnackbar, you can pass an OptionsObject to customize the snackbar's behavior.
Key options:
variant: The visual style of the snackbar. Supported values: 'default' | 'error' | 'success' | 'warning' | 'info'.anchorOrigin: Position of the snackbar. Format: { vertical: 'top' | 'bottom', horizontal: 'left' | 'center' | 'right' }.autoHideDuration: Time in milliseconds before the snackbar closes automatically (default: 5000). Set to null to disable.persist: If true, the snackbar stays on screen until manually dismissed (default: false).preventDuplicate: If true, ignores requests to show a snackbar with the same message (default: false).action: A SnackbarAction (React node or a function returning a node) to display buttons in the snackbar.transitionDuration: Customizes transition timing. Can be a single number (ms) or an object: { enter: number, exit: number }.When upgrading to v2, be aware of the following removals and deprecations:
content prop is deprecated and will be removed in future releases. Use the Components prop for custom content instead.ariaAttributes prop (use custom components for aria-attributes).resumeHideDuration, onEntering, and onExisting.onClose no longer returns reason: 'clickaway'.classes.variant(Success|Error|Info|Warning). Use the Components prop for variant-specific styling.SnackbarProvider to wrap your application, enqueueSnackbar to trigger a new notification, and closeSnackbar to dismiss existing ones.