【问题标题】:how to show progress value in React如何在 React 中显示进度值
【发布时间】:2018-11-27 23:10:18
【问题描述】:

this image after set state progress我尝试在上传图片时使用进度条显示百分比,但不起作用(它只显示在console.log()上)

fileuploadHandler = () => {
const storageRef = fire.storage().ref();
this.state.file.forEach((file) => {
  this.setState({ uploading: true })
  storageRef
    .child(`images/${file.name}`)
    .put(file).then((snapshot) => {
      var uploadTask = storageRef.child(`images/${file.name}`).put(file);
    uploadTask.on('state_changed', function(snapshot){
      var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
      this.setState({progress});
      console.log('Upload is ' + progress + '% done');
      switch (snapshot.state) {
        case fire.storage.TaskState.PAUSED: // or 'paused'
          console.log('Upload is paused');
          break;
        case fire.storage.TaskState.RUNNING: // or 'running'
          console.log('Upload is running');
          break;
      }

<div className='container'>
      {this.state.uploading
        ? <div>
          <div className='load-bar' />
          <progress   value = {this.state.progress} min = "0" max="100" id="uploader">0%</progress>
          <br/><br/>
          <span>Uploading: {this.state.uploader}%</span>
          <h3> {this.state.progress} </h3>


        </div>

【问题讨论】:

    标签: javascript reactjs progress-bar progress


    【解决方案1】:

    您错过了setState 声明。您需要在每次更新时更新state of progress

      var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        this.setState({progress});//add this one
    

    由于progress 没有状态值更新,它没有显示在 UI 上。

    编辑:

    将进度的初始值设置为0

    this.state = {
        progress : 0
      }
    

    编辑 2:

    uploadTask.on('state_changed',(snapshot)=>{
    

    【讨论】:

    • @BankPongsathorn 如果对您有用,请接受答案。
    • 是的,它的工作,但上传完成前进度条是 100%。
    • 我的意思是 this.setState({progress});不能在uploadTask.on('state_changed', function(snapshot){下使用
    • 这是绑定问题。检查 EDIT 2
    • 好的,它的工作!非常感谢,接下来我想为每个文件上传拆分进度条,因为它用于多次上传,但现在它有 1 个进度条,如何解决这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2023-04-09
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    相关资源
    最近更新 更多