首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在React Native中呈现数据的空数组

尝试在React Native中呈现数据的空数组
EN

Stack Overflow用户
提问于 2019-11-14 12:17:35
回答 1查看 154关注 0票数 0

我正在写一个react原生应用。我能够理解如何使用axios或fetch从api数据中检索数据。但是我遇到了麻烦。我正在尝试使用我从上一个类传递的令牌,并使用它来获取新的api和数据。我可以连接到服务器,并且我在邮递员中连接正常。但是当我在Visual Studio中从终端查看我的日志时。我得到一个空数组。我得到array = []。但我知道这是用来显示数据的。

代码语言:javascript
复制
async componentDidMount(){
try {
  const savedProfile = await AsyncStorage.getItem('userData');
  const profile = JSON.parse(savedProfile);
  const token = profile.token;
  console.log(token);
  const email = profile.user_email;
  const name = profile.user_nicename;
  fetch(url, {
    method: 'GET',
    headers:{
      Accept: 'application/json',
               'Content-Type': 'application/json',
       },
           Authorization: "Bearer " + token,
  })
  .then(response => response.json())
  .then(response => console.log(response));
} catch (err) {
  console.warn(err);
}
};

我相信这个问题可能和我在这个网站上读到的其他帖子一样。我也看了react网站上的文档,但我可能需要更多的帮助。我首先要做的就是打印控制台中的数据。我用虚拟数据填充了我的页面。但如果可能的话,我希望用json数据的特定部分填充我的表。基本上是获取地址,这是一个例子。

"address": "4060 18th Ave NE, Naples, Florida, USA",

我渲染的其余部分看起来像这样。但我希望至少在控制台中显示数据,这样我就可以从那里开始。

代码语言:javascript
复制
render() {
return (
  <View>
    <Header
      centerComponent={{ text: 'Schedule', style: { color: '#fff' } }}
      containerStyle={{
        backgroundColor: '#000000',
        justifyContent: 'space-around',
      }}
    />
    <FlatList
      data={[
        {key: 'Devin'},
        {key: 'Dan'},
        {key: 'Dominic'},
        {key: 'Jackson'},
        {key: 'James'},
        {key: 'Joel'},
        {key: 'John'},
        {key: 'Jillian'},
        {key: 'Jimmy'},
        {key: 'Julie'},
      ]}
      renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
    />
  </View>
);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-15 02:34:02

以正确的格式传入头部参数,格式如下:

代码语言:javascript
复制
headers: { 
   'Accept': 'application/json', 
   'Content-Type': 'application/json', 
   'Authorization': 'Bearer ' + token, 
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58849305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档