HookToast.tsx 842 B

1234567891011121314151617181920212223242526272829303132333435
  1. import React, { useState, useEffect } from 'react';
  2. import Toast from '../toast';
  3. import { ToastInstance } from '@douyinfe/semi-foundation/toast/toastFoundation';
  4. interface HookToastProps extends ToastInstance{
  5. afterClose: (id: string) => void;
  6. }
  7. const HookToast = ({ afterClose, ...config }: HookToastProps, ref: React.Ref<any>) => {
  8. const [visible, setVisible] = useState(true);
  9. const close = () => {
  10. setVisible(false);
  11. };
  12. React.useImperativeHandle(ref, () => ({
  13. close: () => {
  14. setVisible(false);
  15. }
  16. }));
  17. useEffect(() => {
  18. if (!visible) {
  19. afterClose(config.id);
  20. }
  21. }, [visible]);
  22. return visible ? (
  23. <Toast
  24. {...config}
  25. close={close}
  26. />
  27. ) : null;
  28. };
  29. export default React.forwardRef(HookToast);