上传React

时间:2019-11-13 15:52:08

标签: reactjs react-hooks filepond

好吧,我的智慧到此为止。我在使用Hooks的功能组件中将FilePond与React一起使用。我不使用FilePond来处理实际的上载,只是使用文件设置状态,我的简化版本是这样:

文件池:

        <FilePond          
            onupdatefiles={fileItems => handleFilepondUpdate(fileItems)}            
          />
        </Form.Field>

手柄更新:

 const handleFilepondUpdate = fileItems => {
    if (fileItems.length === 0) {
      addAttachment({
        ...picture,
        bugAttachment: null
      });
    } else {
      addAttachment({
        ...picture,
        bugAttachment: fileItems[0].file
      });   
    }
  };

状态:

const [picture, addAttachment] = useState({
    bugAttachment: ""
  });
const { bugAttachment } = picture;

最后是我的上载并清除输入状态:

 const onSubmit = e => {
    e.preventDefault();
    const fd = new FormData();
    fd.append("email", props.user[0].email);
    fd.append("bugDescription", bugDescription);
    fd.append("bugAttachment", bugAttachment);
    addBug(fd).then(() => {
      setBug({
        bugDescription: ""
      });

    });
  };

那么发送表单后如何删除FilePond文件?

1 个答案:

答案 0 :(得分:1)

尝试使用addAttachment钩子清除bugAttachment中的onSubmit属性

 const onSubmit = e => {
    e.preventDefault();
    const fd = new FormData();
    fd.append("email", props.user[0].email);
    fd.append("bugDescription", bugDescription);
    fd.append("bugAttachment", bugAttachment);
    addBug(fd).then(() => {
      setBug({
        bugDescription: ""
      });
      addAttachment({
        ...picture,
        bugAttachment:""
      });  

    });
  };

更新: 看来您尚未在图片状态中使用 files 道具,请尝试类似的操作。

 <FilePond          
   files={bugAttachment}
   onupdatefiles={fileItems => handleFilepondUpdate(fileItems)}            
   />