Skip to content
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

Can't close the notistack when set the action option ? #406

Closed
160905 opened this issue Jul 16, 2021 · 5 comments
Closed

Can't close the notistack when set the action option ? #406

160905 opened this issue Jul 16, 2021 · 5 comments

Comments

@160905
Copy link

160905 commented Jul 16, 2021

import React, { Fragment } from 'react';
import { useSnackbar } from 'notistack';
import { IconButton } from '@material-ui/core';
import { HighlightOffOutlined } from '@material-ui/icons';
import { connect } from 'umi';

let displayed: Array<string | number> = [];

interface NotifierProps {
  dispatch: (arg: { type: string; key: number }) => void;
  notifications: Array<any>;
}

const Notifier: React.FC<NotifierProps> = (props) => {
  const { dispatch, notifications } = props;

  const { enqueueSnackbar, closeSnackbar } = useSnackbar();

  const storeDisplayed = (id: number) => {
    displayed = [...displayed, id];
  };

  const removeDisplayed = (id: number) => {
    displayed = [...displayed.filter((key) => id !== key)];
  };

  React.useEffect(() => {
    const action = (key: number | string) => (
      <Fragment>
        <IconButton size="small" onClick={() => closeSnackbar(key)} component="span">
          <HighlightOffOutlined fontSize="small" style={{ color: 'white' }} />
        </IconButton>
      </Fragment>
    );

    notifications.forEach(({ key, message, options = {}, dismissed = false }) => {
      if (dismissed) {
        closeSnackbar(key);
        return;
      }

      if (displayed.includes(key)) return;

      enqueueSnackbar(message, {
        key,
        ...options,
        action,
        persist: false,
        onClose: (event, reason, myKey) => {
          if (options.onClose) {
            options.onClose(event, reason, myKey);
          }
        },
        onExited: (event, myKey: number) => {
          dispatch({ type: 'notistack/REMOVE_SNACKBAR', key: myKey });
          removeDisplayed(myKey);
        },
      });

      storeDisplayed(key);
    });
  }, [notifications, closeSnackbar, enqueueSnackbar, dispatch]);

  return null;
};

export default connect((storeState: any) => ({
  notifications: storeState.notistack.notifications,
}))(Notifier);

At this version
"notistack": "^1.0.9"

@160905
Copy link
Author

160905 commented Jul 16, 2021

this is my git adress https://gitlab.com/zhuzhennan/link-admin

@debajit-saha
Copy link

I am also facing the same issue. It works with Material UI 4.11.4 but not with the latest version 4.12.0.

@iamhosseindhv
Copy link
Owner

You can use npm i notistack@next for now, or wait until #408 is merged and published.

@160905
Copy link
Author

160905 commented Jul 19, 2021

When i use yarn add notistack@next and it work already. Thanks

@Thuplek
Copy link

Thuplek commented Jul 20, 2021

#407 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants