getFiles() 与 relayjs 突变反应本机

getFiles() in react native with relayjs mutations

我需要从 React Native 应用程序抛出 relayjs 突变下载多个文件。文件数组如下所示:

[ {uri: 'file:///path/to/file.jpg', mime: 'image/jpg'}, {uri: 'file:///path/to/file2.jpg', mime: 'image/jpg'}, ]

我发现 getFiles() 函数用于突变,但我不明白它应该是什么格式的数据return?

我是这样解决的:

我使用这个包 https://github.com/jeanpan/react-native-camera-roll-picker 从相机胶卷中获取图像:

你可以用它来测试,把这个添加到你的渲染中:

<CameraRollPicker
          callback={this.getSelectedImage}
          imagesPerRow={4}
          imageMargin={1}
          maximum={1}
        />

这是我对getSelectedImage的实现,我只发送一张图片,但你可以发送多张图片,文件是一个对象,其中的值是一张图片。

getSelectedImage = (images) => {
    const image = images[0];

    const files = {
      [image.filename]: {
        uri: image.uri,
        name: image.filename,
      },
    };

    const mutation = new UploadUserProfileMutation({
      files,
    });

    this.props.relay.commitUpdate(mutation, callbacks);
  };

在你的突变中添加方法 getFiles:

export default class UploadUserProfileMutation extends Relay.Mutation {
...
  getFiles() {
    return this.props.files;
  }
...
}

你应该像正常突变一样完成突变