How to reset ReactJS file input

后端 未结 10 982
傲寒
傲寒 2020-12-29 00:55

I have file upload input:


And I handle upload t

10条回答
  •  礼貌的吻别
    2020-12-29 01:36

    What worked for me was setting a key attribute to the file input, then when I needed to reset it I update the key attribute value:

    functionThatResetsTheFileInput() {
      let randomString = Math.random().toString(36);
    
      this.setState({
        theInputKey: randomString
      });
    }
    
    render() {
      return(
        
    ) }

    That forces React to render the input again from scratch.

提交回复
热议问题