2

Recently I write my React JS app to import images from outside and process that image. In order to handle the user onChange event, I use onChange to run my function handleChange, but it won't work for the second time. How can I handle the onChange event properly? Here is my code

 <div className={classes.fileUpload}> <div style={{ position: "relative", display: "inline-block", height: "100%", width: "100%", }} > <input type={"file"} id="file" accept={"image/png, image/jpeg"} style={{ display: "none" }} onChange={(e) => handleChange(e.target.files)} /> <label htmlFor="file" className={classes.labelFile}> <PublishIcon style={{ marginRight: "10px" }} /> Upload file </label> </div> </div> 
3
  • Are you choosing the same file the second time? Commented Jul 28, 2020 at 4:43
  • have u tried onInput? Commented Jul 28, 2020 at 4:44
  • Thank you, onInput its work <3 Thank you Commented Jul 28, 2020 at 4:53

1 Answer 1

1

try this one

import React from 'react' class SimpleReactFileUpload extends React.Component { constructor(props) { super(props); this.state ={ file:null } this.onChange = this.onChange.bind(this) } onChange(e) { this.setState({file:e.target.files[0]}) } render() { return ( <form onSubmit={this.onFormSubmit}> <h1>File Upload</h1> <input type="file" onChange={this.onChange} /> </form> ) } } export default SimpleReactFileUpload 

hope it will work!

Sign up to request clarification or add additional context in comments.

1 Comment

that kind of complicated way to do this. I tried onInput and its work, thank you btw

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.