[英]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.