setState error on NotificationStack
Version Number: 6.6.0
Test Case
Add vast of notification at the same time
Steps to Reproduce
Repeatedly click button addNotification
Expected Behavior
Don't appear warning in inspect
Actual Behavior
Appear Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the StackedNotification component
Does this error occur for the example?
@pburtchaell yes, this error occur in example, too
Thanks!
Repeatedly click button addNotification
Can you provide more code? How are you using the component?
This is my Notification component
import React, { Component } from 'react';
import { NotificationStack } from 'react-notification';
import { OrderedSet } from 'immutable';
class Notification extends Component {
state = {
notifications: OrderedSet(),
// This is just used for the sake of an example to make sure
// notifications have unique keys. In production, you should have
// a different system for UIDs.
count: 0,
};
addNotification = (message) => {
const { notifications, count } = this.state;
const newCount = count + 1;
return this.setState({
count: newCount,
notifications: notifications.add({
message: message,
key: newCount,
dismissAfter: 5000,
onClick: () => this.removeNotification(newCount),
})
});
}
removeNotification = (count) => {
const { notifications } = this.state;
this.setState({
notifications: notifications.filter(n => n.key !== count)
});
}
render () {
let { notifications } = this.state;
return (
<div>
<NotificationStack
notifications={notifications.toArray()}
onDismiss={notification => this.setState({
notifications: notifications.delete(notification)
})}
/>
</div>
);
}
}
export default Notification;
and use it in
import React, { Component } from 'react';
import { Button } from 'semantic-ui-react';
import Notification from 'components/Notification';
class Register extends Component {
onClick = () => {
let { notification } = this.refs;
notification.addNotification("This is sample notification");
}
render() {
return (
<div>
<Notification ref='notification' />
<Button primary onClick={this.onClick}>Show Notification</Button>
</div>
);
}
}
export default Register;
Click button Show Notification and wait for several second and warning will be occur
It is difficult to parse your code. Can you reduce your code to only what code is needed to reproduce the error? I'm going to close this issue for now because I can't reproduce on my end.
It's showing the same error for me too. Reproducing was easy, I cloned your repo and ran it, opened the Notification Tree example and clicked on the button for like 100 times. (It's already showing the warning for less notifications, but if you want to make sure.)

Thank you for taking the time, I like this plugin.
Just installed this component today and I'm seeing this setState error when using NotificationStack. Why is the issue closed?
when using NotificationStack
You're not giving me any information that I can use to resolve your issue. Can you provide more information, e.g., example code?
Hi @shantp, I encountered the same issue, after a bit of digging in the code, I found that this is due to the NotificationStack will add 1 second to the dismissAfter:
File notificationStack.js line 37: if (!dismissNow) dismissAfter += index * 1000;
This incremented dismissAfter will be used to create StackedNotification. So in file stackedNotification.js line 38: this.dismissTimeout = setTimeout(this.setState.bind(this, {
isActive: false
}), dismissAfter); will have incremented dismissAfter.
But we will execute onDismiss to remove the notification component with original dismissAfter, the component will be removed before the later this.setState.bind... is called.
For example: if the I create a NotificationStack with dismissAfter 5 seconds, the created notification component will be unmounted after 5 seconds, but the this.setState.bind(... may run after 6 seconds due to the dismissAfter += index * 1000. So it causes setState on unmounted component error.
A work around is to add dismissInOrder={false} on NotificationStack, so it will not add 1 second to the dismissAfter value.
Please let me know if I made any mistake on the findings.
p.s. this is a great plugin, I love it.
@haolinj thanks for digging into that. Your workaround looks like solves our issue for now.
@pburtchaell based on the above, I think this issue should be reopened so someone can take a look and submit a PR with a solution to handle this case without a workaround. Thoughts?