首页 > 解决方案 > react-native:如何检查是否所有图像都添加到数组中

问题描述

我正在使用 react-nativeImagePicker选择照片,然后将其保存在AWS S3. 每件事都很好,但我有一个问题。

问题: 我选择3 images使用ImagePicker,我正在加载activity indicator图像上传的时间。我想关掉activity indicator所有的3 images are added to my array

但我不知道如何检查是否所有 3 个图像都添加到数组中。

这是我的代码:

ImagePicker.openPicker({
      multiple: true,
      maxFiles: 3
    }).then(response => {

   ...

    let image = {
            uri: response.uri,
            width: newWidth,
            height: newHeight,
            name: _fileName,
            type: 'image/png'
          }
          const config = {

            bucket: 'bucket',
            region: 'ap-northeast-2',
            accessKey: 'mykey',
            secretKey: 'secretkey',
            successActionStatus: 201
          }

    RNS3.put(image, config)
            .then(responseFromS3 => {

              this.setState({
                imageUrl: [...this.state.imageUrl, responseFromS3.body.postResponse.location]
              }, 
//Every time when image is added the call back below is called. Which is what I don't want.
//I want the call back to be called when all three images are added the the array.   
()=> {this.setState({activityIndicator:false})
//I want to change the status of my activityIndicator to false when all 3 images are added to the array.
//However, this.setState({activityIndicator:false}) is called when one Image is added the to array.
                      })
                    })
                  }).catch((err) => {
                   console.log(err);
                 })
               })
            }).catch(err => {
              this.setState({loadingImageGifVisible:false})
            })
          }

标签: javascriptreactjsreact-native

解决方案


推荐阅读