如何在类组件中使用useRef和react-toastify

时间:2020-06-19 07:35:00

标签: reactjs use-ref react-toastify create-ref

我已将react-toastify成功集成到功能组件中的onUploadProgress中。 我无法在类组件中获得该功能,因为由于引用它每次都会创建一个新的吐司。 如何在反应中使用useRef或createRef是否有其他解决方案。

此功能正常

 const toastId = React.useRef(null);
 onUploadProgress: p => {
    const progress = p.loaded / p.total;

    // check if we already displayed a toast
    if(toastId.current === null){
        toastId = toast('Upload in Progress', {
        progress: progress
      });
    } else {
      toast.update(toastId.current, {
        progress: progress
      })
    }
  }
}

这不起作用,正在反复烤面包

const toastId = React.createRef(null);

onUploadProgress: p => {
    const progress = p.loaded / p.total;

    // check if we already displayed a toast
    if(this.toastId.current === null){
        this.toastId = toast('Upload in Progress', {
        progress: progress
      });
    } else {
      toast.update(this.toastId.current, {
        progress: progress
      })
    }
  }
}

0 个答案:

没有答案
相关问题