在类方法 React Native 中包装 fetch API

奥卢瓦科雷德·法绍昆

为了让我的 React Native 代码更加结构化,在我最新的项目中,我决定创建一个类来保存所有 fetch 请求。类的结构见下图:

import { AsyncStorage } from 'react-native';

// const API_URL = 'http://localhost:5500';
const API_URL = 'https://trail-api.herokuapp.com';

export default class Backend {
  static logIn(username, password){
    fetch(`${API_URL}/users/login`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      },
      body: {
        username: username,
        password: password
      }
    });
  }
  static signUp(firstname, lastname, username, email, password){
    fetch(`${API_URL}/users/register`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      },
      body: {
        firstname: firstname,
        lastname: lastname,
        username: username,
        email: email,
        password: password,
        confirmpassword: password
      }
    });
  }
  static getUser(){
    AsyncStorage.getItem('@Trail:user');
  }
  static getFeed(username){
    fetch(`${API_URL}/user/${username}/feed`);
  }
  static logOut(user){
    AsyncStorage.removeItem('@Trail:user');
  }
  static followUser(user, username){
    fetch(`${API_URL}/users/${username}/follow`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      }
    });
  }
  static createPost(user, text, longitude, latitude){
    fetch(`${API_URL}/posts/new`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      },
      user: user,
      body: {
        text: text,
        location: {
          longitude: longitude,
          latitude: latitude
        }
      }
    });
  }
  static likePost(user, id){
    fetch(`${API_URL}/posts/${id}/like`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      },
      user: user,
      params: {
        id: id
      }
    });
  }
  static unlikePost(user, id){
    fetch(`${API_URL}/posts/${id}/unlike`, {
      method: 'POST',
      headers: {
        Accept: 'application/json'
      },
      user: user
    });
  }
  static getComments(id){
    fetch(`${API_URL}/posts/${id}/comments`, {
      method: 'GET',
      headers: {
        Accept: 'application/json'
      }
    });
  }
}

如果我像这样调用它是任何组件,我希望代码可以工作:

async login(){
  const { username, password } = this.state;
  await Backend.logIn(username, password)
    .then((response) => response.json())
    .then((responseJSON) => AsyncStorage.setItem('@Trail:user', 
        JSON.stringify(responseJSON.user)));
    () => navigate('Home');
}

但相反,它会抛出一个 Promise 拒绝错误。有什么我想念的吗?更新:请查看下图以了解错误:

提取的错误代码。

原型

你能不能尝试像这样返回 fetch (这样你就可以在之后实际捕获 Promise):

 static logIn(username, password){
    return fetch(`${API_URL}/users/login`, {
          method: 'POST',
          headers: {
            Accept: 'application/json'
          },
          body: {
            username: username,
            password: password
          }
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在 React Native fetch 方法中解析 JSON 不起作用

来自分类Dev

.catch() 在 react-native 的 fetch 中

来自分类Dev

如何使用.fetch函数使API调用IPV6在react-native中兼容?

来自分类Dev

React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

来自分类Dev

如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

来自分类Dev

如何在 React 的类组件的实例方法中模拟 fetch?

来自分类Dev

React Native Fetch异步

来自分类Dev

React Native Fetch API不返回我的调用

来自分类Dev

如何使用React Native Fetch()从API检索格式错误的json

来自分类Dev

React Native Fetch API调用未发出请求

来自分类Dev

React Native - 如何从我的 fetch 方法中创建一个全局变量

来自分类Dev

Activity类中的调用方法(React Native Android)

来自分类Dev

钩在React Native的类中

来自分类Dev

react native中的axios和fetch有什么区别?

来自分类Dev

react-native 如何从另一个包装在 react-redux 中的 connect() 类调用函数?

来自分类Dev

React Native fetch,意外的令牌

来自分类Dev

React Native fetch 在另一个 fetch 回调中不起作用

来自分类Dev

用于 iOS 和 android 库的 React-Native 包装类

来自分类Dev

在React Native中连接REST API

来自分类Dev

React Native 中基于 Api 的布局

来自分类Dev

如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

来自分类Dev

react-native-fetch-blob IOS API openDocument 不起作用

来自分类Dev

从外部访问类方法-React Native

来自分类Dev

无法在React Native中使用`fetch()`

来自分类Dev

对 Microsoft Azure 的 React Native fetch 请求失败

来自分类Dev

React Native fetch vs XMLHttpRequest 性能

来自分类Dev

C ++类中的成功回调Emscripten FETCH API

来自分类Dev

在React Native中存储来自JSON API的大数据收集的最佳方法

来自分类Dev

使用React Native中的Fetch使用查询字符串进行GET

Related 相关文章

  1. 1

    在 React Native fetch 方法中解析 JSON 不起作用

  2. 2

    .catch() 在 react-native 的 fetch 中

  3. 3

    如何使用.fetch函数使API调用IPV6在react-native中兼容?

  4. 4

    React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

  5. 5

    如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

  6. 6

    如何在 React 的类组件的实例方法中模拟 fetch?

  7. 7

    React Native Fetch异步

  8. 8

    React Native Fetch API不返回我的调用

  9. 9

    如何使用React Native Fetch()从API检索格式错误的json

  10. 10

    React Native Fetch API调用未发出请求

  11. 11

    React Native - 如何从我的 fetch 方法中创建一个全局变量

  12. 12

    Activity类中的调用方法(React Native Android)

  13. 13

    钩在React Native的类中

  14. 14

    react native中的axios和fetch有什么区别?

  15. 15

    react-native 如何从另一个包装在 react-redux 中的 connect() 类调用函数?

  16. 16

    React Native fetch,意外的令牌

  17. 17

    React Native fetch 在另一个 fetch 回调中不起作用

  18. 18

    用于 iOS 和 android 库的 React-Native 包装类

  19. 19

    在React Native中连接REST API

  20. 20

    React Native 中基于 Api 的布局

  21. 21

    如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

  22. 22

    react-native-fetch-blob IOS API openDocument 不起作用

  23. 23

    从外部访问类方法-React Native

  24. 24

    无法在React Native中使用`fetch()`

  25. 25

    对 Microsoft Azure 的 React Native fetch 请求失败

  26. 26

    React Native fetch vs XMLHttpRequest 性能

  27. 27

    C ++类中的成功回调Emscripten FETCH API

  28. 28

    在React Native中存储来自JSON API的大数据收集的最佳方法

  29. 29

    使用React Native中的Fetch使用查询字符串进行GET

热门标签

归档