繁体   English   中英

React,通过添加与状态相关的类来有条件地设置正文样式

[英]React, set body style conditionally by adding a class related to a state

我有以下内容,我试图在上传图片时更改正文样式(因此用户知道它正在上传)。 如何在图像上传时将类附加到正文,然后在图像上传后将其删除?

const [uploadingImage, setUploadingImage] = useState(false)

    const uploadToFirebase = e => {
        e.preventDefault();
        if (image) {
          const storageRef = storage.ref(`${user1}/${tripid}/documents`);
          const imageRef = storageRef.child(image.name);
          
          setUploadingImage(true)
          imageRef.put(image)
            .then((url) => {
              alert("Successfully uploaded.");
              getFromFirebase();
              setUploadingImage(false)
            })
            .catch(e => setUploadingImage(false));
        } else {
          alert("Please upload an image first.");
        }
      };

您可以使用useEffect ,如下所示:

useEffect(()=>{
  document.body.classList.toggle("image-uploading", uploadingImage);
},[uploadingImage])

然后在你的css中添加这个:

body.image-uploading{
  /* whatever style you want goes here*/
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM