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>

最佳答案

您错过了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)=>{

关于javascript - 如何在React中显示进度值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50920747/

10-12 00:20
查看更多