New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enqueueSnackbar multiple times ignores maxSnack #38
Comments
Sorry for the delay, been scrambling with the holidays coming up. I'll try to get a sandbox reproduction up here in the next day or two. |
Any update? |
Closed due to inactivity. feel free to reopen anytime. |
I have the same issue ... mobx store starts with 4 items which are enqueued during the notifier mount ... all 4 are displayed even though maxSnack=1 on the SnackProvider. I thought it would internally queue the notifications when more are enqueued than the max. If I have persist on I see this in the console:
|
I'll reopen and investigate this. |
Hi, I've the same problem. When I try to show a snackbar, at the first time, it will not declare and at the second time it shows two times. Can you help me how can I fix this? |
@MortezaHeydari97 please share the code you have to enqueue snackbar. |
I fixed it, so thanks. |
I have the same issue, My code sandbox here: https://codesandbox.io/s/notistack-redux-example-gyw9u |
We have the same issue as tomzaku. It seems that all configurations get ignored when adding snackbar while iterating through an array. I tried to fix the issue by setting |
A minimal example for this bug: https://codesandbox.io/s/notistack-simple-example-xpff1 (click on the default button). Important lines: handleClickWithAction = () => {
this.props.enqueueSnackbar("Customise this snackbar youself.");
this.props.enqueueSnackbar("Customise this snackbar youself.");
this.props.enqueueSnackbar("Customise this snackbar youself.");
this.props.enqueueSnackbar("Customise this snackbar youself.");
}; The bug appears when the enqueueSnackbar method is called multiple times in one synchronous method, or before the React tree re-renders. I believe the origin of the bug lies in the following code (from SnackbarProvider.js) /**
* Display snack if there's space for it. Otherwise, immediately begin dismissing the
* oldest message to start showing the new one.
*/
handleDisplaySnack = () => {
const { maxSnack } = this.props;
const { snacks } = this.state;
if (snacks.length >= maxSnack) {
return this.handleDismissOldest();
}
return this.processQueue();
};
/**
* Display items (notifications) in the queue if there's space for them.
*/
processQueue = () => {
if (this.queue.length > 0) {
const newOne = this.queue.shift();
this.setState(({ snacks }) => ({
snacks: [...snacks, newOne],
}));
}
}; When However, the bug appears because setState is asynchronous: at the beginning of the above
In my view, a solution for this error would require to handle this.setState((prevState) => ({
if (prevState.snacks.length >= maxSnack)
return handleDimissOldest(prevState)
return processQueue(prevState)
})); Any thoughts @iamhosseindhv ? |
correct @simonbos. The issue originates in asynchronous nature of setState. #15 tried and somewhat managed to solve the issue. If you follow the discussion in that thread you'll find out why it didn't get merged. But I like the general idea in #15 and this issue is on top of my todo list. |
Expected Behavior
I'm using CustomEvents to determine when to show the snackbar. On the home page I have some temp methods to test using this library.
The provider in
App.tsx
:<SnackbarProvider maxSnack={3} anchorOrigin={snackbarOrigin}>...
I have a HoC using
withSnackbar(Component)
with the following event handler:On the home page I have 5 buttons that fire off events that get picked up by that HoC (Test All, Test Info, Test Warning, Test Error, Test Success). The single event buttons work as I would expect when I randomly click them several times. I expected clicking the "Test All" button would show 3, then show the 4th when the first one auto-hides.
Current Behavior
If I click the "All Alerts" button, which instantly fires 4 events, I'm seeing all 4 snackbar items show at the same time, then all disappear at the same time.
Steps to Reproduce
Set the provider max to 3, then make a click event that runs
enqueueSnackbar
4+ times in a row with no pause in between.BTW this template no longer exists?
This should be pretty easy to reproduce, but I can create a codesandbox if needed.
Context
The reason I'm testing this scenario: I was using a custom mobx store to do what this library does, but wanted to try this out. Since this is used when API calls error (among other things), there are times where you might have several events fire in rapid succession.
Your Environment
Project is based on create-react-app using TypeScript. Running on Windows 10, vscode. Everything is current version.
| Tech | Version |
The text was updated successfully, but these errors were encountered: