小编MUH*_*BAL的帖子

如何在类组件中使用 React.useRef()?

我需要在类组件中实现这段代码。这是为了在我的类组件中使用 react-toastify 的上传进度

function Example(){
  const toastId = React.useRef(null);
  function handleUpload(){
    axios.request({
      method: "post", 
      url: "/foobar", 
      data: myData, 
      onUploadProgress: p => {
        const progress = p.loaded / p.total;
        if(toastId.current === null){
            toastId = toast('Upload in Progress', {
            progress: progress
          });
        } else {
          toast.update(toastId.current, {
            progress: progress
          })
        }
      }
    }).then(data => {
      
      toast.done(toastId.current);
    })
  }
  return (
    <div>
      <button onClick={handleUpload}>Upload something</button>
    </div>
  )
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

reactjs react-toastify

8
推荐指数
2
解决办法
2万
查看次数

标签 统计

react-toastify ×1

reactjs ×1