How to reset ReactJS file input










8















I have file upload input:



<input onChange=this.getFile id="fileUpload" type="file" className="upload"/>


And I handle upload this way:



getFile(e) 
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = (theFile) =>
var data =
blob: theFile.target.result, name: file.name,
visitorId: this.props.socketio.visitorId
;
console.log(this.props.socketio);
this.props.socketio.emit('file-upload', data);
;
reader.readAsDataURL(file);



If I upload same file twice, then upload event is not fired. How can I fix that? For simple js code it was enough to do the following: this.value = null; in change handler. How can I do it with ReactJS?










share|improve this question
























  • onChange=this.getFile.bind(this) or getFile = (e) => {

    – Dmitriy Kovalenko
    Feb 12 '17 at 19:56















8















I have file upload input:



<input onChange=this.getFile id="fileUpload" type="file" className="upload"/>


And I handle upload this way:



getFile(e) 
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = (theFile) =>
var data =
blob: theFile.target.result, name: file.name,
visitorId: this.props.socketio.visitorId
;
console.log(this.props.socketio);
this.props.socketio.emit('file-upload', data);
;
reader.readAsDataURL(file);



If I upload same file twice, then upload event is not fired. How can I fix that? For simple js code it was enough to do the following: this.value = null; in change handler. How can I do it with ReactJS?










share|improve this question
























  • onChange=this.getFile.bind(this) or getFile = (e) => {

    – Dmitriy Kovalenko
    Feb 12 '17 at 19:56













8












8








8


1






I have file upload input:



<input onChange=this.getFile id="fileUpload" type="file" className="upload"/>


And I handle upload this way:



getFile(e) 
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = (theFile) =>
var data =
blob: theFile.target.result, name: file.name,
visitorId: this.props.socketio.visitorId
;
console.log(this.props.socketio);
this.props.socketio.emit('file-upload', data);
;
reader.readAsDataURL(file);



If I upload same file twice, then upload event is not fired. How can I fix that? For simple js code it was enough to do the following: this.value = null; in change handler. How can I do it with ReactJS?










share|improve this question
















I have file upload input:



<input onChange=this.getFile id="fileUpload" type="file" className="upload"/>


And I handle upload this way:



getFile(e) 
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = (theFile) =>
var data =
blob: theFile.target.result, name: file.name,
visitorId: this.props.socketio.visitorId
;
console.log(this.props.socketio);
this.props.socketio.emit('file-upload', data);
;
reader.readAsDataURL(file);



If I upload same file twice, then upload event is not fired. How can I fix that? For simple js code it was enough to do the following: this.value = null; in change handler. How can I do it with ReactJS?







html5 reactjs file-upload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 14 '18 at 7:09









hlfrmn

1,0821221




1,0821221










asked Feb 12 '17 at 19:42









Stepan YakovenkoStepan Yakovenko

1,1421255114




1,1421255114












  • onChange=this.getFile.bind(this) or getFile = (e) => {

    – Dmitriy Kovalenko
    Feb 12 '17 at 19:56

















  • onChange=this.getFile.bind(this) or getFile = (e) => {

    – Dmitriy Kovalenko
    Feb 12 '17 at 19:56
















onChange=this.getFile.bind(this) or getFile = (e) => {

– Dmitriy Kovalenko
Feb 12 '17 at 19:56





onChange=this.getFile.bind(this) or getFile = (e) => {

– Dmitriy Kovalenko
Feb 12 '17 at 19:56












7 Answers
7






active

oldest

votes


















21














I think you can just clear the input value like this :



e.target.value = null;


File input cannot be controlled, there is no React specific way to do that.






share|improve this answer

























  • awesome... :) it saved my day

    – DirtyMind
    Dec 20 '17 at 20:35


















16














This work for me - ref=ref => this.fileInput = ref



<input id="file_input_file" type="file" onChange=(e) => this._handleFileChange(e) ref=ref=> this.fileInput = ref />


then in my case once the file was uploaded to the server , I clear it by using the statement below



 this.fileInput.value = "";





share|improve this answer























  • I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

    – pesho hristov
    Feb 4 at 12:38


















12














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(
<div>
<input type="file"
key= '' />
<button onClick=this.functionThatResetsTheFileInput() />
</div>
)



That forces React to render the input again from scratch.






share|improve this answer























  • To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

    – Jaspal Singh
    Dec 6 '17 at 21:54











  • I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

    – Alexander Falk
    Sep 5 '18 at 9:12











  • I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

    – pesho hristov
    Feb 4 at 12:39


















1














You can also include this in your input element if you know you are not going to be using the built-in file input value at all.



<input value="" ... />


This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.






share|improve this answer






























    0














    I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.



    constructor(props) 
    super(props);
    this.state =
    selectedFile: undefined,
    selectedFileName: undefined,
    imageSrc: undefined,
    value: ''
    ;

    this.handleChange = this.handleChange.bind(this);
    this.removeImage = this.removeImage.bind(this);


    handleChange(event)
    if (event.target.files[0])
    this.setState(
    selectedFile: event.target.files[0],
    selectedFileName: event.target.files[0].name,
    imageSrc: window.URL.createObjectURL(event.target.files[0]),
    value: event.target.value,
    );



    // Call this function to reset input
    removeImage()
    this.setState(
    selectedFile: undefined,
    selectedFileName: undefined,
    imageSrc: undefined,
    value: ''
    )


    render()
    return (
    <input type="file" value=this.state.value onChange=this.handleChange />
    );






    share|improve this answer
































      0














      Here is my solution using redux form



      class FileInput extends React.Component 
      constructor()
      super();

      this.deleteImage = this.deleteImage.bind(this);


      deleteImage()
      // Just setting input ref value to null did not work well with redux form
      // At the same time just calling on change with nothing didn't do the trick
      // just using onChange does the change in redux form but if you try selecting
      // the same image again it doesn't show in the preview cause the onChange of the
      // input is not called since for the input the value is not changing
      // but for redux form would be.

      this.fileInput.value = null;
      this.props.input.onChange();


      render()
      const input: onChange, value , accept, disabled, error = this.props;
      const edited = this.state;

      return (
      <div className="file-input-expanded">
      /* ref and on change are key properties here */
      <input
      className="hidden"
      type="file"
      onChange=e => onChange(e.target.files[0])
      multiple=false
      accept=accept
      capture
      ref=(input) => this.fileInput = input;
      disabled=disabled
      />
      !value ?
      /* Add button */
      <Button
      className="btn-link action"
      type="button"
      text="Add Image"
      onPress=() => this.fileInput.click()
      disabled=disabled
      />
      :
      <div className="file-input-container">
      <div className="flex-row">
      /* Image preview */
      <img src=window.URL.createObjectURL(value) alt="outbound MMS" />
      <div className="flex-col mg-l-20">
      /* This button does de replacing */
      <Button
      type="button"
      className="btn-link mg-b-10"
      text="Change Image"
      onPress=() => this.fileInput.click()
      disabled=disabled
      />
      /* This button is the one that does de deleting */
      <Button
      type="button"
      className="btn-link delete"
      text="Delete Image"
      onPress=this.deleteImage
      disabled=disabled
      />
      </div>
      </div>
      error &&
      <div className="error-message"> error</div>

      </div>

      </div>
      );



      FileInput.propTypes =
      input: object.isRequired,
      accept: string,
      disabled: bool,
      error: string
      ;

      FileInput.defaultProps =
      accept: '*',
      ;

      export default FileInput;





      share|improve this answer






























        -1














        React is nothing but JavaScript and we can use DOM manipulation in React code as well.
        This should be working



        document.getElementsByClassName('upload')[0].value = null;





        share|improve this answer






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42192346%2fhow-to-reset-reactjs-file-input%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          21














          I think you can just clear the input value like this :



          e.target.value = null;


          File input cannot be controlled, there is no React specific way to do that.






          share|improve this answer

























          • awesome... :) it saved my day

            – DirtyMind
            Dec 20 '17 at 20:35















          21














          I think you can just clear the input value like this :



          e.target.value = null;


          File input cannot be controlled, there is no React specific way to do that.






          share|improve this answer

























          • awesome... :) it saved my day

            – DirtyMind
            Dec 20 '17 at 20:35













          21












          21








          21







          I think you can just clear the input value like this :



          e.target.value = null;


          File input cannot be controlled, there is no React specific way to do that.






          share|improve this answer















          I think you can just clear the input value like this :



          e.target.value = null;


          File input cannot be controlled, there is no React specific way to do that.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 12 '17 at 20:28

























          answered Feb 12 '17 at 20:14









          FreezFreez

          3,35211021




          3,35211021












          • awesome... :) it saved my day

            – DirtyMind
            Dec 20 '17 at 20:35

















          • awesome... :) it saved my day

            – DirtyMind
            Dec 20 '17 at 20:35
















          awesome... :) it saved my day

          – DirtyMind
          Dec 20 '17 at 20:35





          awesome... :) it saved my day

          – DirtyMind
          Dec 20 '17 at 20:35













          16














          This work for me - ref=ref => this.fileInput = ref



          <input id="file_input_file" type="file" onChange=(e) => this._handleFileChange(e) ref=ref=> this.fileInput = ref />


          then in my case once the file was uploaded to the server , I clear it by using the statement below



           this.fileInput.value = "";





          share|improve this answer























          • I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

            – pesho hristov
            Feb 4 at 12:38















          16














          This work for me - ref=ref => this.fileInput = ref



          <input id="file_input_file" type="file" onChange=(e) => this._handleFileChange(e) ref=ref=> this.fileInput = ref />


          then in my case once the file was uploaded to the server , I clear it by using the statement below



           this.fileInput.value = "";





          share|improve this answer























          • I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

            – pesho hristov
            Feb 4 at 12:38













          16












          16








          16







          This work for me - ref=ref => this.fileInput = ref



          <input id="file_input_file" type="file" onChange=(e) => this._handleFileChange(e) ref=ref=> this.fileInput = ref />


          then in my case once the file was uploaded to the server , I clear it by using the statement below



           this.fileInput.value = "";





          share|improve this answer













          This work for me - ref=ref => this.fileInput = ref



          <input id="file_input_file" type="file" onChange=(e) => this._handleFileChange(e) ref=ref=> this.fileInput = ref />


          then in my case once the file was uploaded to the server , I clear it by using the statement below



           this.fileInput.value = "";






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 6 '17 at 2:05









          JozcarJozcar

          460510




          460510












          • I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

            – pesho hristov
            Feb 4 at 12:38

















          • I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

            – pesho hristov
            Feb 4 at 12:38
















          I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

          – pesho hristov
          Feb 4 at 12:38





          I like the approach, however I wanted to make this from getDerivedStateFromProps and unfortunately it won't work, as we don't have access to this there.

          – pesho hristov
          Feb 4 at 12:38











          12














          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(
          <div>
          <input type="file"
          key= '' />
          <button onClick=this.functionThatResetsTheFileInput() />
          </div>
          )



          That forces React to render the input again from scratch.






          share|improve this answer























          • To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

            – Jaspal Singh
            Dec 6 '17 at 21:54











          • I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

            – Alexander Falk
            Sep 5 '18 at 9:12











          • I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

            – pesho hristov
            Feb 4 at 12:39















          12














          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(
          <div>
          <input type="file"
          key= '' />
          <button onClick=this.functionThatResetsTheFileInput() />
          </div>
          )



          That forces React to render the input again from scratch.






          share|improve this answer























          • To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

            – Jaspal Singh
            Dec 6 '17 at 21:54











          • I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

            – Alexander Falk
            Sep 5 '18 at 9:12











          • I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

            – pesho hristov
            Feb 4 at 12:39













          12












          12








          12







          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(
          <div>
          <input type="file"
          key= '' />
          <button onClick=this.functionThatResetsTheFileInput() />
          </div>
          )



          That forces React to render the input again from scratch.






          share|improve this answer













          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(
          <div>
          <input type="file"
          key= '' />
          <button onClick=this.functionThatResetsTheFileInput() />
          </div>
          )



          That forces React to render the input again from scratch.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 23 '17 at 17:44









          tonatiuh-Ntonatiuh-N

          9051017




          9051017












          • To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

            – Jaspal Singh
            Dec 6 '17 at 21:54











          • I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

            – Alexander Falk
            Sep 5 '18 at 9:12











          • I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

            – pesho hristov
            Feb 4 at 12:39

















          • To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

            – Jaspal Singh
            Dec 6 '17 at 21:54











          • I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

            – Alexander Falk
            Sep 5 '18 at 9:12











          • I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

            – pesho hristov
            Feb 4 at 12:39
















          To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

          – Jaspal Singh
          Dec 6 '17 at 21:54





          To explain how this works, you need to update the this.state.theInputKey when you want to cleat the input. Under the hood, changing the key causes react to re-render the input thus clearing it.

          – Jaspal Singh
          Dec 6 '17 at 21:54













          I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

          – Alexander Falk
          Sep 5 '18 at 9:12





          I like this idea. Then I can control the input field from other functionalities, which is what I need right now. It works fine. Thanks.

          – Alexander Falk
          Sep 5 '18 at 9:12













          I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

          – pesho hristov
          Feb 4 at 12:39





          I like the approach, however I wanted to make this from getDerivedStateFromProps, and fortunately it works there as well, cause we still have access to the state. :)

          – pesho hristov
          Feb 4 at 12:39











          1














          You can also include this in your input element if you know you are not going to be using the built-in file input value at all.



          <input value="" ... />


          This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.






          share|improve this answer



























            1














            You can also include this in your input element if you know you are not going to be using the built-in file input value at all.



            <input value="" ... />


            This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.






            share|improve this answer

























              1












              1








              1







              You can also include this in your input element if you know you are not going to be using the built-in file input value at all.



              <input value="" ... />


              This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.






              share|improve this answer













              You can also include this in your input element if you know you are not going to be using the built-in file input value at all.



              <input value="" ... />


              This way the value is always reset to the empty string on render and you don't have to include it awkwardly in an onChange function.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 8 at 20:25









              IRTrapGodIRTrapGod

              2115




              2115





















                  0














                  I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.



                  constructor(props) 
                  super(props);
                  this.state =
                  selectedFile: undefined,
                  selectedFileName: undefined,
                  imageSrc: undefined,
                  value: ''
                  ;

                  this.handleChange = this.handleChange.bind(this);
                  this.removeImage = this.removeImage.bind(this);


                  handleChange(event)
                  if (event.target.files[0])
                  this.setState(
                  selectedFile: event.target.files[0],
                  selectedFileName: event.target.files[0].name,
                  imageSrc: window.URL.createObjectURL(event.target.files[0]),
                  value: event.target.value,
                  );



                  // Call this function to reset input
                  removeImage()
                  this.setState(
                  selectedFile: undefined,
                  selectedFileName: undefined,
                  imageSrc: undefined,
                  value: ''
                  )


                  render()
                  return (
                  <input type="file" value=this.state.value onChange=this.handleChange />
                  );






                  share|improve this answer





























                    0














                    I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.



                    constructor(props) 
                    super(props);
                    this.state =
                    selectedFile: undefined,
                    selectedFileName: undefined,
                    imageSrc: undefined,
                    value: ''
                    ;

                    this.handleChange = this.handleChange.bind(this);
                    this.removeImage = this.removeImage.bind(this);


                    handleChange(event)
                    if (event.target.files[0])
                    this.setState(
                    selectedFile: event.target.files[0],
                    selectedFileName: event.target.files[0].name,
                    imageSrc: window.URL.createObjectURL(event.target.files[0]),
                    value: event.target.value,
                    );



                    // Call this function to reset input
                    removeImage()
                    this.setState(
                    selectedFile: undefined,
                    selectedFileName: undefined,
                    imageSrc: undefined,
                    value: ''
                    )


                    render()
                    return (
                    <input type="file" value=this.state.value onChange=this.handleChange />
                    );






                    share|improve this answer



























                      0












                      0








                      0







                      I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.



                      constructor(props) 
                      super(props);
                      this.state =
                      selectedFile: undefined,
                      selectedFileName: undefined,
                      imageSrc: undefined,
                      value: ''
                      ;

                      this.handleChange = this.handleChange.bind(this);
                      this.removeImage = this.removeImage.bind(this);


                      handleChange(event)
                      if (event.target.files[0])
                      this.setState(
                      selectedFile: event.target.files[0],
                      selectedFileName: event.target.files[0].name,
                      imageSrc: window.URL.createObjectURL(event.target.files[0]),
                      value: event.target.value,
                      );



                      // Call this function to reset input
                      removeImage()
                      this.setState(
                      selectedFile: undefined,
                      selectedFileName: undefined,
                      imageSrc: undefined,
                      value: ''
                      )


                      render()
                      return (
                      <input type="file" value=this.state.value onChange=this.handleChange />
                      );






                      share|improve this answer















                      I know file input is always uncontrolled however the following code still works in my own porject, I can reset the input with no problems at all.



                      constructor(props) 
                      super(props);
                      this.state =
                      selectedFile: undefined,
                      selectedFileName: undefined,
                      imageSrc: undefined,
                      value: ''
                      ;

                      this.handleChange = this.handleChange.bind(this);
                      this.removeImage = this.removeImage.bind(this);


                      handleChange(event)
                      if (event.target.files[0])
                      this.setState(
                      selectedFile: event.target.files[0],
                      selectedFileName: event.target.files[0].name,
                      imageSrc: window.URL.createObjectURL(event.target.files[0]),
                      value: event.target.value,
                      );



                      // Call this function to reset input
                      removeImage()
                      this.setState(
                      selectedFile: undefined,
                      selectedFileName: undefined,
                      imageSrc: undefined,
                      value: ''
                      )


                      render()
                      return (
                      <input type="file" value=this.state.value onChange=this.handleChange />
                      );







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 27 '18 at 12:58

























                      answered Jul 27 '18 at 12:16









                      Jack L.Jack L.

                      13




                      13





















                          0














                          Here is my solution using redux form



                          class FileInput extends React.Component 
                          constructor()
                          super();

                          this.deleteImage = this.deleteImage.bind(this);


                          deleteImage()
                          // Just setting input ref value to null did not work well with redux form
                          // At the same time just calling on change with nothing didn't do the trick
                          // just using onChange does the change in redux form but if you try selecting
                          // the same image again it doesn't show in the preview cause the onChange of the
                          // input is not called since for the input the value is not changing
                          // but for redux form would be.

                          this.fileInput.value = null;
                          this.props.input.onChange();


                          render()
                          const input: onChange, value , accept, disabled, error = this.props;
                          const edited = this.state;

                          return (
                          <div className="file-input-expanded">
                          /* ref and on change are key properties here */
                          <input
                          className="hidden"
                          type="file"
                          onChange=e => onChange(e.target.files[0])
                          multiple=false
                          accept=accept
                          capture
                          ref=(input) => this.fileInput = input;
                          disabled=disabled
                          />
                          !value ?
                          /* Add button */
                          <Button
                          className="btn-link action"
                          type="button"
                          text="Add Image"
                          onPress=() => this.fileInput.click()
                          disabled=disabled
                          />
                          :
                          <div className="file-input-container">
                          <div className="flex-row">
                          /* Image preview */
                          <img src=window.URL.createObjectURL(value) alt="outbound MMS" />
                          <div className="flex-col mg-l-20">
                          /* This button does de replacing */
                          <Button
                          type="button"
                          className="btn-link mg-b-10"
                          text="Change Image"
                          onPress=() => this.fileInput.click()
                          disabled=disabled
                          />
                          /* This button is the one that does de deleting */
                          <Button
                          type="button"
                          className="btn-link delete"
                          text="Delete Image"
                          onPress=this.deleteImage
                          disabled=disabled
                          />
                          </div>
                          </div>
                          error &&
                          <div className="error-message"> error</div>

                          </div>

                          </div>
                          );



                          FileInput.propTypes =
                          input: object.isRequired,
                          accept: string,
                          disabled: bool,
                          error: string
                          ;

                          FileInput.defaultProps =
                          accept: '*',
                          ;

                          export default FileInput;





                          share|improve this answer



























                            0














                            Here is my solution using redux form



                            class FileInput extends React.Component 
                            constructor()
                            super();

                            this.deleteImage = this.deleteImage.bind(this);


                            deleteImage()
                            // Just setting input ref value to null did not work well with redux form
                            // At the same time just calling on change with nothing didn't do the trick
                            // just using onChange does the change in redux form but if you try selecting
                            // the same image again it doesn't show in the preview cause the onChange of the
                            // input is not called since for the input the value is not changing
                            // but for redux form would be.

                            this.fileInput.value = null;
                            this.props.input.onChange();


                            render()
                            const input: onChange, value , accept, disabled, error = this.props;
                            const edited = this.state;

                            return (
                            <div className="file-input-expanded">
                            /* ref and on change are key properties here */
                            <input
                            className="hidden"
                            type="file"
                            onChange=e => onChange(e.target.files[0])
                            multiple=false
                            accept=accept
                            capture
                            ref=(input) => this.fileInput = input;
                            disabled=disabled
                            />
                            !value ?
                            /* Add button */
                            <Button
                            className="btn-link action"
                            type="button"
                            text="Add Image"
                            onPress=() => this.fileInput.click()
                            disabled=disabled
                            />
                            :
                            <div className="file-input-container">
                            <div className="flex-row">
                            /* Image preview */
                            <img src=window.URL.createObjectURL(value) alt="outbound MMS" />
                            <div className="flex-col mg-l-20">
                            /* This button does de replacing */
                            <Button
                            type="button"
                            className="btn-link mg-b-10"
                            text="Change Image"
                            onPress=() => this.fileInput.click()
                            disabled=disabled
                            />
                            /* This button is the one that does de deleting */
                            <Button
                            type="button"
                            className="btn-link delete"
                            text="Delete Image"
                            onPress=this.deleteImage
                            disabled=disabled
                            />
                            </div>
                            </div>
                            error &&
                            <div className="error-message"> error</div>

                            </div>

                            </div>
                            );



                            FileInput.propTypes =
                            input: object.isRequired,
                            accept: string,
                            disabled: bool,
                            error: string
                            ;

                            FileInput.defaultProps =
                            accept: '*',
                            ;

                            export default FileInput;





                            share|improve this answer

























                              0












                              0








                              0







                              Here is my solution using redux form



                              class FileInput extends React.Component 
                              constructor()
                              super();

                              this.deleteImage = this.deleteImage.bind(this);


                              deleteImage()
                              // Just setting input ref value to null did not work well with redux form
                              // At the same time just calling on change with nothing didn't do the trick
                              // just using onChange does the change in redux form but if you try selecting
                              // the same image again it doesn't show in the preview cause the onChange of the
                              // input is not called since for the input the value is not changing
                              // but for redux form would be.

                              this.fileInput.value = null;
                              this.props.input.onChange();


                              render()
                              const input: onChange, value , accept, disabled, error = this.props;
                              const edited = this.state;

                              return (
                              <div className="file-input-expanded">
                              /* ref and on change are key properties here */
                              <input
                              className="hidden"
                              type="file"
                              onChange=e => onChange(e.target.files[0])
                              multiple=false
                              accept=accept
                              capture
                              ref=(input) => this.fileInput = input;
                              disabled=disabled
                              />
                              !value ?
                              /* Add button */
                              <Button
                              className="btn-link action"
                              type="button"
                              text="Add Image"
                              onPress=() => this.fileInput.click()
                              disabled=disabled
                              />
                              :
                              <div className="file-input-container">
                              <div className="flex-row">
                              /* Image preview */
                              <img src=window.URL.createObjectURL(value) alt="outbound MMS" />
                              <div className="flex-col mg-l-20">
                              /* This button does de replacing */
                              <Button
                              type="button"
                              className="btn-link mg-b-10"
                              text="Change Image"
                              onPress=() => this.fileInput.click()
                              disabled=disabled
                              />
                              /* This button is the one that does de deleting */
                              <Button
                              type="button"
                              className="btn-link delete"
                              text="Delete Image"
                              onPress=this.deleteImage
                              disabled=disabled
                              />
                              </div>
                              </div>
                              error &&
                              <div className="error-message"> error</div>

                              </div>

                              </div>
                              );



                              FileInput.propTypes =
                              input: object.isRequired,
                              accept: string,
                              disabled: bool,
                              error: string
                              ;

                              FileInput.defaultProps =
                              accept: '*',
                              ;

                              export default FileInput;





                              share|improve this answer













                              Here is my solution using redux form



                              class FileInput extends React.Component 
                              constructor()
                              super();

                              this.deleteImage = this.deleteImage.bind(this);


                              deleteImage()
                              // Just setting input ref value to null did not work well with redux form
                              // At the same time just calling on change with nothing didn't do the trick
                              // just using onChange does the change in redux form but if you try selecting
                              // the same image again it doesn't show in the preview cause the onChange of the
                              // input is not called since for the input the value is not changing
                              // but for redux form would be.

                              this.fileInput.value = null;
                              this.props.input.onChange();


                              render()
                              const input: onChange, value , accept, disabled, error = this.props;
                              const edited = this.state;

                              return (
                              <div className="file-input-expanded">
                              /* ref and on change are key properties here */
                              <input
                              className="hidden"
                              type="file"
                              onChange=e => onChange(e.target.files[0])
                              multiple=false
                              accept=accept
                              capture
                              ref=(input) => this.fileInput = input;
                              disabled=disabled
                              />
                              !value ?
                              /* Add button */
                              <Button
                              className="btn-link action"
                              type="button"
                              text="Add Image"
                              onPress=() => this.fileInput.click()
                              disabled=disabled
                              />
                              :
                              <div className="file-input-container">
                              <div className="flex-row">
                              /* Image preview */
                              <img src=window.URL.createObjectURL(value) alt="outbound MMS" />
                              <div className="flex-col mg-l-20">
                              /* This button does de replacing */
                              <Button
                              type="button"
                              className="btn-link mg-b-10"
                              text="Change Image"
                              onPress=() => this.fileInput.click()
                              disabled=disabled
                              />
                              /* This button is the one that does de deleting */
                              <Button
                              type="button"
                              className="btn-link delete"
                              text="Delete Image"
                              onPress=this.deleteImage
                              disabled=disabled
                              />
                              </div>
                              </div>
                              error &&
                              <div className="error-message"> error</div>

                              </div>

                              </div>
                              );



                              FileInput.propTypes =
                              input: object.isRequired,
                              accept: string,
                              disabled: bool,
                              error: string
                              ;

                              FileInput.defaultProps =
                              accept: '*',
                              ;

                              export default FileInput;






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 15:14









                              Agustina ChaerAgustina Chaer

                              62




                              62





















                                  -1














                                  React is nothing but JavaScript and we can use DOM manipulation in React code as well.
                                  This should be working



                                  document.getElementsByClassName('upload')[0].value = null;





                                  share|improve this answer



























                                    -1














                                    React is nothing but JavaScript and we can use DOM manipulation in React code as well.
                                    This should be working



                                    document.getElementsByClassName('upload')[0].value = null;





                                    share|improve this answer

























                                      -1












                                      -1








                                      -1







                                      React is nothing but JavaScript and we can use DOM manipulation in React code as well.
                                      This should be working



                                      document.getElementsByClassName('upload')[0].value = null;





                                      share|improve this answer













                                      React is nothing but JavaScript and we can use DOM manipulation in React code as well.
                                      This should be working



                                      document.getElementsByClassName('upload')[0].value = null;






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 10 '18 at 6:58









                                      Rastko SasicRastko Sasic

                                      1




                                      1



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42192346%2fhow-to-reset-reactjs-file-input%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          這個網誌中的熱門文章

                                          How to read a connectionString WITH PROVIDER in .NET Core?

                                          Museum of Modern and Contemporary Art of Trento and Rovereto

                                          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully