【问题标题】:How to display JSON data in FlatList React Native如何在 FlatList React Native 中显示 JSON 数据
【发布时间】:2018-10-01 09:16:31
【问题描述】:

我向我的 API 发送请求 它返回我将其转换为 JSON 的响应,如下所示

现在我想在 FlaList 中显示这个 JSON

我正在尝试这样做,但它没有显示任何内容

<FlatList

          data={this.state.dataSource}
          renderItem={({item}) => <Text> {item.Begin} </Text> }

【问题讨论】:

  • 你能分享你的console.log(...)吗?
  • @lsaac 我在图片中分享它
  • 那是console.log 的结果,我的意思是console.log 的确切代码我需要看看你的变量
  • pressBtn = () => { var x = this.state.dataSource;控制台.log(x); } @lsaac

标签: json reactjs react-native


【解决方案1】:

您需要将 Text 标签放入 View 标签和 keyExtractor

 import React, { Component } from 'react';
 import { Platform, StyleSheet, Text, View, FlatList, Image, ScrollView, Picker } from 'react-native';

 export default class Test extends Component {
  constructor() {
  super();
   this.state = {
    data : [
    {"Begin" : "08:00:00" ,End : "08:10:00"},
    {"Begin" : "08:10:00" ,End : "08:20:00"},
    {"Begin" : "08:20:00" ,End : "08:30:00"},
    {"Begin" : "08:30:00" ,End : "08:40:00"},
    {"Begin" : "08:40:00" ,End : "08:50:00"},
    {"Begin" : "08:50:00" ,End : "08:60:00"},
  ]
  }
}

_keyExtractor = (item, index) => item.Begin.toString();

render() {

 return (
   <View style={{flex:1,justifyContent:'center'}}>
   <FlatList
   data={this.state.data}
   keyExtractor={this._keyExtractor}
   renderItem={({item}) => <Text> {item.Begin} </Text> }
   />
   </View>
  )
 }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多