简体   繁体   English

从变量React-Native将数据膨胀到FlatList

[英]Inflate data to FlatList from a variable React-Native

I am storing a JSON file in the local storage using react-native-fs and fetching the data from the JSON file and storing it in a variable. 我正在使用react-native-fs将JSON文件存储在本地存储中,并从JSON文件中获取数据并将其存储在变量中。

I want to fetch the data stored in the variable and inflate it on a FlatList. 我想获取存储在变量中的数据并在FlatList上对其进行充气。

I have tried 我努力了

// getting data from the local file
var path = RNFS.DocumentDirectoryPath + '/test.json';
return RNFS.readFile(path, 'utf8')
  .then((success) => {
      console.log(success);//Data is storing successfully see console output below
      this.setState({        
      isLoading: false,
      dataSource: success.recordset //data is not getting separated with respect to recordset 
    });
    console.log(dataSource);//see outpout below
  })
  .catch((err) => {
    console.log(err.message);
  });

console.log(success) output console.log(成功)输出

[{"recordset":[[{"id":1,"UPRN":552,"SiteName":"County Hall","DueDate":"2019-04-26T00:00:00.000Z","SurveyStatus":"Completed","SyncStatus":"Completed"},{"id":2,"UPRN":554,"SiteName":"County Hall 2","DueDate":"2019-03-01T00:00:00.000Z","SurveyStatus":"Completed","SyncStatus":"sync-in-progress"},{"id":3,"UPRN":1524,"SiteName":"County Hall 3","DueDate":"2019-03-02T00:00:00.000Z","SurveyStatus":"Survey-in-progress","SyncStatus":null},{"id":4,"UPRN":2546,"SiteName":"County Hall 4","DueDate":"2019-03-15T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":5,"UPRN":2156,"SiteName":"County Hall 5","DueDate":"2019-07-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":6,"UPRN":8945,"SiteName":"County Hall 6","DueDate":"2019-06-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":7,"UPRN":5214,"SiteName":"County Hall 7","DueDate":"2020-06-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null}]] [{“ recordset”:[[{“ id”:1,“ UPRN”:552,“ SiteName”:“ County Hall”,“ DueDate”:“ 2019-04-26T00:00:00.000Z”,“ SurveyStatus” :“已完成”,“ SyncStatus”:“已完成”},{“ id”:2,“ UPRN”:554,“ SiteName”:“ County Hall 2”,“ DueDate”:“ 2019-03-01T00:00: 00.000Z“,” SurveyStatus“:”已完成“,” SyncStatus“:”正在同步“},{” id“:3,” UPRN“:1524,” SiteName“:” County Hall 3“,” DueDate “:”“ 2019-03-02T00:00:00.000Z”,“ SurveyStatus”:“进行中的调查”,“ SyncStatus”:null},{“ id”:4,“ UPRN”:2546,“ SiteName” :“ County Hall 4”,“ DueDate”:“ 2019-03-15T00:00:00.000Z”,“ SurveyStatus”:null,“ SyncStatus”:null},{“ id”:5,“ UPRN”:2156, “ SiteName”:“ County Hall 5”,“ DueDate”:“ 2019-07-01T00:00:00.000Z”,“ SurveyStatus”:null,“ SyncStatus”:null},{“ id”:6,“ UPRN” :8945,“ SiteName”:“ County Hall 6”,“ DueDate”:“ 2019-06-01T00:00:00.000Z”,“ SurveyStatus”:null,“ SyncStatus”:null},{“ id”:7, “ UPRN”:5214,“ SiteName”:“ County Hall 7”,“ DueDate”:“ 2020-06-01T00:00:00.000Z”,“ SurveyStatus”:null,“ SyncStatus”:null}]]

console.log(dataSource) output console.log(dataSource)输出

dataSource is not defined 未定义dataSource

Flatlist code 平面代码

    <FlatList
      data={this.state.dataSource}
      renderItem={({item}) =>
      <View style={styles.flatview}>
        <Text style={styles.name}>{item.UPRN}</Text>
        <Text style={styles.email}>{item.SiteName}</Text>
        <Text style={styles.email}>{item.DueDate}</Text>
        <Text style={styles.email}>{item.SurveyStatus}</Text>
        <Text style={styles.email}>{item.SyncStatus}</Text>
      </View>
      }
      keyExtractor={item => item.id}
    />

How do I inflate this data in FlatList with respect to id? 我该如何在FlatList中针对ID填充此数据?

There's a few things going on here. 这里发生了一些事情。

First, your dataSource is not defined message is because the console.log statement is outside the scope of the RNFS.readFile result. 首先,您的dataSource is not defined消息是因为console.log语句不在RNFS.readFile结果的范围内。

Second you need to parse the JSON to convert the contents of the json file to objects, for example: 其次,您需要解析JSON以将json文件的内容转换为对象,例如:

JSON.parse(success).then(result => this.setState({dataSource, result[0].recordset})

Third it looks like recordset is in an array which is why it's referenced above as result[0].recordset . 第三,它看起来recordset位于数组中,这就是为什么它在上面被引用为result[0].recordset

Just do it like 像那样做


var path = RNFS.DocumentDirectoryPath + '/test.json';
return RNFS.readFile(path, 'utf8')
  .then((success) => {
      console.log(success);//Data is storing successfully see console output below

   const dataSource = success[0].recordset[0]

 this.setState({        
      isLoading: false,
      dataSource
    });
    console.log(dataSource);//see outpout below
  })
  .catch((err) => {
    console.log(err.message);
  });

the error is because you are using the datasource and it is undefined . 该错误是因为您正在使用数据源,并且它是未定义的。 you need to define the variable you are using. 您需要定义您正在使用的变量。

and try to format your json array to 并尝试将您的json数组格式化为

{"recordset":[{"id":1,"UPRN":552,"SiteName":"County Hall","DueDate":"2019-04-26T00:00:00.000Z","SurveyStatus":"Completed","SyncStatus":"Completed"},{"id":2,"UPRN":554,"SiteName":"County Hall 2","DueDate":"2019-03-01T00:00:00.000Z","SurveyStatus":"Completed","SyncStatus":"sync-in-progress"},{"id":3,"UPRN":1524,"SiteName":"County Hall 3","DueDate":"2019-03-02T00:00:00.000Z","SurveyStatus":"Survey-in-progress","SyncStatus":null},{"id":4,"UPRN":2546,"SiteName":"County Hall 4","DueDate":"2019-03-15T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":5,"UPRN":2156,"SiteName":"County Hall 5","DueDate":"2019-07-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":6,"UPRN":8945,"SiteName":"County Hall 6","DueDate":"2019-06-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null},{"id":7,"UPRN":5214,"SiteName":"County Hall 7","DueDate":"2020-06-01T00:00:00.000Z","SurveyStatus":null,"SyncStatus":null}]}

because you are accessing the wrong variable 因为您访问错误的变量

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM