简体   繁体   English

反应:输入 onChange 不会触发第二次,第一次工作

[英]React: input onChange won't fire for the second time, first time it's work

Recently I write my React JS app to import images from outside and process that image.最近我编写了我的 React JS 应用程序来从外部导入图像并处理该图像。 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.为了处理用户onChange事件,我使用 onChange 运行我的 function handleChange ,但它不会第二次工作。 How can I handle the onChange event properly?如何正确处理onChange事件? 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>

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!希望它会奏效!

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

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