简体   繁体   English

如何从 fetch 中获取 uri?

[英]How can I get the uri from the fetch?

I want to cache images in the app, so they can load faster.我想在应用程序中缓存图像,以便它们可以更快地加载。 The tutorial I found used FileSystem from Expo, but I'm not using expo, so I found react-native-file-access instead.我发现的教程使用了 Expo 的FileSystem ,但我没有使用 expo,所以我找到了react-native-file-access After mounting, the code should check if the image exists, if not, download the image.挂载后,代码应该检查图像是否存在,如果不存在,则下载图像。 I've tried but I can't seem to make it work.我试过了,但我似乎无法让它工作。

import React, {Component} from 'react';
import {Image} from 'react-native';
import shorthash from 'shorthash';
import {Dirs, FileSystem} from 'react-native-file-access';

export default class CacheImage extends Component {
  state = {
    source: null,
  };

  componentDidMount = async () => {
    const {uri} = this.props;
    const name = shorthash.unique(uri);
    const path = `${Dirs.CacheDir}/${name}`;

    const image = await FileSystem.exists(path);
    if (image.exists) {
      this.setState({
        source: {
          uri: image.uri,
        },
      });
      return;
    }

    /* Download image if one doesn't exist */
    const newImage = await FileSystem.fetch(uri, {path: path})
      .then(response => {
        JSON.stringify(response);
      })
      .then(data => data);

    this.setState({
      source: {
        uri: newImage.uri,
      },
    });
    console.log('URI: ', this.state.source);
  };

  render() {
    return <Image style={this.props.style} source={this.state.source} />;
  }
}

the problem come from here问题来自这里

const newImage = await FileSystem.fetch(uri, {path: path})
      .then(response => {
        JSON.stringify(response);
      })
      .then(data => data);

you forget to return when use {}使用时忘记返回 {}

const newImage = await FileSystem.fetch(uri, {path: path})
      .then(response => {
        return JSON.stringify(response);
      })
      .then(data => data);

I returned the response without the JSON.stringify() .我返回了没有JSON.stringify()response

const newImage = await FileSystem.fetch(uri, {path: path})
      .then(response => {
        return response;
      });

this.setState({
      source: {
        uri: newImage.url,
      },
    });

And I also removed the second .then() because I still get the url without adding it.而且我还删除了第二个.then()因为我仍然得到url而不添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ApexCharts 获取 URI 以下载 PDF? - How can I get a URI from ApexCharts to download in PDF? 如何使用 React Js 从 ApexCharts 获取 URI - How can I get URI from ApexCharts with React Js 我如何从PHP Fetch Assoc获取结果 - How can i get result from PHP fetch assoc 在reactjs中,我如何从其他文件的fetch()获取响应 - In reactjs how can I get the response from fetch() in other file 如何在我的角度应用程序中获取clientID和重定向URI以从onedrive访问文件 - How can I get the clientID and redirect URI to access files from onedrive in my angular application 如何获取URI参数-使用JavaScript查询字符串? - How can I get URI parameters - query string with JavaScript? 如何从 Fetch 转换为 axios? 我收到 401 错误 - How can I convert from Fetch to axios? I get 401 error 如何从URI创建StorageFile对象? - How can I create a StorageFile object from URI? 我如何从 base64 字符串或文件 uri 或字节数组中获取实际文件类型? - how can i get actual file type from base64 String or file uri or bytearray in react native or js? 如何使用angular 2管道从get请求中获取数据? - How I can fetch data from get request using angular 2 pipe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM