简体   繁体   English

如何使用 GraphQL 和 React Native 从 GitHub 获取你的名字?

[英]How to get your name from GitHub using GraphQL and React Native?

I'm trying to get my GitHub username using GraphQL and React Native.我正在尝试使用 GraphQL 和 React Native 获取我的 GitHub 用户名。 I keep getting the error "Objects are not valid as a React child."我不断收到错误“对象作为 React 子对象无效”。 I have tried declaring an array and appending the results to that array but then I just get an empty array.我曾尝试声明一个数组并将结果附加到该数组,但后来我得到了一个空数组。 Therefore, I'm not 100% sure if I'm fetching the data correctly.因此,我不能 100% 确定我是否正确获取了数据。 I have also tried putting curly braces around mainFunction() as well as every suggested solution I could find on the internet but nothing seems to work.我也试过在 mainFunction() 周围加上花括号,以及我在互联网上能找到的每一个建议的解决方案,但似乎没有任何效果。 How can I get it working?我怎样才能让它工作?

import React, { Component } from 'react';
import { Text, View } from 'react-native';
import { Navigation } from 'react-native-navigation';
import { ApolloClient } from 'apollo-boost';

var asynchronousFunction = async () => {
  var response = await fetch('https://api.github.com/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'bearer ' + {TOKEN},
    },
    body: '{ "query": "{ viewer { name } } "}',
  });
  return response;
};

var mainFunction = async () => {
  var result = await asynchronousFunction();
  return result;
};

(async () => {
  console.log(await mainFunction());
})();

class Example extends Component {
  render() {
    return (
      <View>
        <Text>{JSON.stringify(mainFunction())}</Text>
      </View>
    );
  }
}

export default Example;

The problem is here问题就在这里

 render() {
    return (
      mainFunction()
    );
  }

The return statement from mainFunction is not a react component so it would always be an object so you should display is inside a Text using JSON stringify Something like below mainFunction 的 return 语句不是反应组件,因此它始终是一个对象,因此您应该使用 JSON stringify 在文本中显示如下内容

 render() {
    return (
      <View><Text>{JSON.stringify(mainFunction())}</Text></View>
    );
  }

Still its better to have a state and update it once you get the response which is the best practice when it comes to react最好有一个状态并在收到响应后更新它,这是响应时的最佳实践

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

相关问题 React Native 使用图像选择器时如何获取图像名称? - React Native How to get the image name when using image picker? 如何使用react从github API获取用户仓库? - How to get the user repo from github API using react? 如何使用react和react-apollo从graphql服务器获取数据? - how to get data from graphql server using react and react-apollo? 使用 Graphql 和 Github API 获取仓库信息 - Using Graphql and Github API to get repository information 如何在反应原生的 GraphQL 缓存中存储 object - How to store object in GraphQL cache in react native 如何从 React Native 应用程序在 GitHub 上提交问题? - How can I submit an issue on GitHub from React Native application? 如何在 React 中使用来自 aws 的 GraphQL Get 查询 - How to use the GraphQL Get query from aws in React 从github导入模块以响应本机 - Importing module from github to react native 如何使用带有本地触发器onPress()的触发器的react-navigation从FlatList react-native获取详细数据? - How to get detail data from the FlatList react-native using react-navigation with the trigger is button onPress() from native base? 让本机如何从承诺中获得回报 - react native how to get return from a promise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM