Comments (13)
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 preventDuplicate
totrue
and I still get multiple snackbars with the same message. Can be replicated in tomzaku's sandbox by adding preventDuplicate: true
.
from notistack.
@Kizmar Thanks for reporting this. I'll investigate this. It'd be nice to have a sandbox reproduction though.
I'll update sandbox link in issue template to this.
from notistack.
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.
from notistack.
Any update?
from notistack.
Closed due to inactivity. feel free to reopen anytime.
from notistack.
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:
Reached maxSnack while all enqueued snackbars have 'persist' flag. Notistack will dismiss the oldest snackbar anyway to allow other ones in the queue to be presented.
from notistack.
I'll reopen and investigate this.
from notistack.
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?
from notistack.
@MortezaHeydari97 please share the code you have to enqueue snackbar.
from notistack.
@MortezaHeydari97 please share the code you have to enqueue snackbar.
I fixed it, so thanks.
from notistack.
I have the same issue, My code sandbox here: https://codesandbox.io/s/notistack-redux-example-gyw9u
How can I solve that
from notistack.
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 handleDisplaySnack
is called, the snacks
array is fetched from the state and checked if the length of the array is greater or equal than maxSnack
. If not, the state adds a new snack from the queue.
However, the bug appears because setState is asynchronous: at the beginning of the above handleClickWithAction
, the length of const { snacks } = this.state;
is 0. At the end, after the four enqueueSnackbar invocations it will be still 0, and it will remain that way untill the setState calls are effectively executed. The relevant React docs:
setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall.
In my view, a solution for this error would require to handle snacks.length >= maxSnack
in the setState reducer, i.e. something like:
this.setState((prevState) => ({
if (prevState.snacks.length >= maxSnack)
return handleDimissOldest(prevState)
return processQueue(prevState)
}));
Any thoughts @iamhosseindhv ?
from notistack.
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.
Any PR welcome.
from notistack.
Related Issues (20)
- NextJs server component support - enqueueSnackbar is not a function (missing "use client" directive) HOT 1
- Feature Request
- Can we use the existing snackbar to use the notistack
- Styles not applied when rendering in custom element with shadow dom HOT 2
- domRoot is ineffective HOT 2
- [help] How can I make new snackbar always appears at the start position instead of at the rear? HOT 2
- Document has multiple elements referenced with ARIA with the same id attribute: notistack-snackbar
- autoHideDuration values not respected in Chrome 116.0.5845.179 HOT 2
- Feature request: option to bypass `maxSnack` HOT 2
- Question is it possible to identify if there is snack on the screen
- JS Example for Custom Notistack HOT 1
- Feature request: compiled react / pure JS documentation
- How to trigger "close Snack bar" from custom snack bar component. HOT 3
- Update clsx version
- snackbar id value appears on the screen
- Impossible to tell if user intentionally closed a notification
- Type issue when extending SnackbarProvider Components with custom component HOT 6
- The global settings on the provider doesn't work HOT 4
- [Documentation] Update custom snackbar component example HOT 2
- Incorrectly generated styles only in production build
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from notistack.