react-notification icon indicating copy to clipboard operation
react-notification copied to clipboard

setState error on NotificationStack

Open sonhgc00016 opened this issue 9 years ago • 10 comments

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

sonhgc00016 avatar Jan 05 '17 04:01 sonhgc00016

Does this error occur for the example?

pburtchaell avatar Jan 05 '17 04:01 pburtchaell

@pburtchaell yes, this error occur in example, too

sonhgc00016 avatar Jan 05 '17 05:01 sonhgc00016

Thanks!

Repeatedly click button addNotification

Can you provide more code? How are you using the component?

pburtchaell avatar Jan 05 '17 05:01 pburtchaell

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

sonhgc00016 avatar Jan 05 '17 07:01 sonhgc00016

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.

pburtchaell avatar Jan 20 '17 01:01 pburtchaell

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.)

notif

Thank you for taking the time, I like this plugin.

JuhG avatar Jan 20 '17 08:01 JuhG

Just installed this component today and I'm seeing this setState error when using NotificationStack. Why is the issue closed?

shantp avatar Apr 21 '17 22:04 shantp

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?

pburtchaell avatar Apr 22 '17 19:04 pburtchaell

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 avatar Apr 26 '17 07:04 haolinj

@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?

brianespinosa avatar Jul 06 '17 20:07 brianespinosa