简体   繁体   English

有谁知道如何使这个上传的 img function 与旧版本的 reactJS 兼容?

[英]Does anyone know how to make this upload img function compatible with an older version of reactJS?

I am new to using reactJS (and JS in general).我不熟悉使用 reactJS(和一般的 JS)。 I am trying to add a new function to an existing project but it is not compatible because the react dependency is 16.13.1 and does not use hooks apparently.我正在尝试向现有项目添加一个新的 function 但它不兼容,因为反应依赖项是 16.13.1 并且显然不使用挂钩。 It doesn't accept useState() in the version of the project.它不接受项目版本中的 useState()。

function App() {
 const [file, setFile] = useState(); // storing the uploaded file
 function handleChange(e) { // function to handle the file upload
 console.log(e.target.files);
 setFile(URL.createObjectURL(e.target.files[0]));
  }

 return (
 <div className="App">
 <h2>Add Image:</h2>
 <input type="file" onChange={handleChange} />
 <img src={file} />

 </div>

  );
}

export default App;

I want to make this compatible with that dependency but I have had no luck with my attempts yet.我想让它与那个依赖兼容,但我的尝试还没有成功。 does anyone know how to make it compatible or any resources that can help?有谁知道如何使其兼容或任何可以提供帮助的资源? sorry if this is an obvious question, I still have a lot to learn!对不起,如果这是一个明显的问题,我还有很多东西要学! thank you!谢谢你!

What I've tried so far:到目前为止我已经尝试过:

class uploadImage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            subMenuOpen: false,
            file: null 
        }
        this.handleChange = this.handleChange.bind(this)
    }
handleChange = (e) => {
        this.setState({
            file: URL.createObjectURL(e.target.files[0])
        })
    }
render () {
  return (
      <div className="App">
          <h2>Add Image:</h2>
          <input type="file" onChange={handleChange} />
          <img src={file} />

      </div>

  );
}}

maybe you should convert your function into Class Component也许您应该将 function 转换为Class Component

try change <img src={file} /> to <img src={this.state.file} />尝试将<img src={file} />更改为<img src={this.state.file} />

<input type="file" onChange={handleChange} /> to <input type="file" onChange={this.handleChange} /> <input type="file" onChange={handleChange} /><input type="file" onChange={this.handleChange} />

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

相关问题 如何使 4.X Typescript 项目与旧版本的 Typescript 3.X 兼容? - How to make 4.X Typescript project compatible with older version of Typescript 3.X? 有谁知道如何使图像出现然后消失? - Does anyone know how to make an image appear and then dissapear? 有谁知道如何使观看状态与 url 一起使用? - Does anyone know how to make watching status work with url? 有谁知道如何为jQuery cycle插件设置回调函数? - Does anyone know how to set a callback function for the jQuery cycle plugin? 有谁知道如何使Javascript的“ onunload”功能正常工作吗? - Does ANYone know how to get the Javascript ‘onunload’ function to work? 有谁知道一个适用于网络应用程序的免费批量上传工具? - Does anyone know of a good free bulk upload tool for web apps? 如何使应用程序可以在旧版本的节点上运行 - How to make application runnable on older version of node 有谁知道如何格式化Jquery Datepicker - Does anyone know how to format the Jquery Datepicker 有谁知道如何使起始编号和终止编号产生无序的平方根列表? - Does anyone know how to make a start number and end number produce a unordered list of square roots? Karma.js:有谁知道如何使业力返回日志记录/错误的文件名 - Karma.js: Does anyone know how to make karma return filename of logging / errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM