【问题标题】:how to convert react class into react hooks(firebase storage)如何将反应类转换为反应钩子(firebase 存储)
【发布时间】:2019-12-06 11:41:02
【问题描述】:

我很难将 react 类组件转换为钩子。 我已经有一段时间没有玩 react 了,所以我无法掌握它。

我的代码试图做的是上传图像并将其放入 Firebase 存储并显示它。 它适用于类组件,但我无法将它们转换为钩子。 有人可以帮我吗???

import React, {Component} from 'react';
import fire from '../config/Fire';
import FileUploader from 'react-firebase-file-uploader';
import firebase from 'firebase';



class Fileupload extends Component {

  state = {
    image: '',
    imageURL: '',
    progress: 0
  }


  handleUploadStart = () => {

    this.setState({
      progress: 0
    })
  }

  handleUploadSuccess = filename => {
    this.setState ({
      image: filename,
      progress: 100
    })

    fire.storage().ref('avatars').child(filename).getDownloadURL()
    .then(url => this.setState({
      imageURL: url
    }))
  }



  render() {

    console.log("this.state", this.state)
    console.log("this.state.imageURL",this.state.imageURL)
  return (
    <div className="App">
      <h2>file upload and load</h2>

      <FileUploader  
      accept="image/*"
      name='image'
      storageRef={fire.storage().ref('avatars')}
      onUploadStart = {this.handleUploadStart}
      onUploadSuccess = {this.handleUploadSuccess}
      />
<div>
    <h4>show image</h4>
    <img src={this.state.imageURL} />

</div>


    </div>
  );
};
  }
export default Fileupload;

【问题讨论】:

    标签: javascript reactjs firebase firebase-storage


    【解决方案1】:

    在将类重构为钩子时要记住几件事。 - 而不是Class 使用function()() =&gt; arrow function, - 没有渲染函数只有一个返回语句, - this 关键字仅用于将函数和状态绑定到Class。这些不在函数中使用, - 你应该使用useState 钩子而不是this.setState

    import React, { useState } from 'react';
    import fire from '../config/Fire';
    import FileUploader from 'react-firebase-file-uploader';
    import firebase from 'firebase';
    
    function Fileupload() {
      const [image, setImage] = useState('');
      const [imageURL, setImageUrl] = useState('');
      const [progress, setProgress] = useState(0);
    
      const handleUploadStart = () => {
        setProgress(0);
      };
    
      const handleUploadSuccess = filename => {
        setImage(filename);
        setProgress(100);
    
        fire
          .storage()
          .ref('avatars')
          .child(filename)
          .getDownloadURL()
          .then(url => setImageUrl(url));
      };
    
      return (
        <div className="App">
          <h2>file upload and load</h2>
    
          <FileUploader
            accept="image/*"
            name="image"
            storageRef={fire.storage().ref('avatars')}
            onUploadStart={handleUploadStart}
            onUploadSuccess={handleUploadSuccess}
          />
          <div>
            <h4>show image</h4>
            <img src={imageURL} />
          </div>
        </div>
      );
    }
    
    export default Fileupload;
    

    【讨论】:

    • 非常感谢您的帮助。对此,我真的非常感激。我试图在一个 const 中使用 [upload img progress ] 的 const 将其命名为 upload 并在其中放入处理程序。我现在知道我做错了。我应该根据您提供的答案进行学习。你帮了大忙:)))
    • 酷,很高兴我能帮上忙!你能接受我的回答吗? :)
    猜你喜欢
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2019-09-01
    • 2020-08-06
    • 2022-01-23
    • 2020-02-14
    • 2021-02-05
    • 2021-04-22
    相关资源
    最近更新 更多