带有 Json Api 的 ReactNative ListView

2024-03-09

我无法在列表视图中显示 json 数据。我在 console.log 中获取 json 数据,但在 listview 中获取不到 isLoading 始终为 false。 我没有收到任何错误 .catch(error => console.warn("error"))。 屏幕上的结果是第一个视图,因为 this.state.isLoading 为 false。

这是一个代码:

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, ListView, Text, View,Image,TouchableHighlight } from 'react-native';

var productArray = [];

class ListViewDemo extends Component {
   constructor(props) {
    console.warn("constructor");
    super(props);
    var dataSource = new ListView.DataSource({rowHasChanged:(r1,r2) => r1.guid != r2.guid});
   this.state = {
      dataSource: dataSource.cloneWithRows(productArray),
     isLoading:true
   }
 }

  componentDidMount() {
    console.warn("componentDidMount");
    this.getTheData(function(json){
     productArray = json;
      console.warn(productArray);
     this.setState = ({
       datasource:this.state.dataSource.cloneWithRows(productArray),
       isLoading:false
     })
    }.bind(this));   
     console.warn("component ->  " + this.state.isLoading);
  }

  getTheData(callback) {
    console.warn("callback");
    var url = "https://raw.githubusercontent.com/darkarmyIN/React-Native-DynamicListView/master/appledata.json";
fetch(url)
     .then(response => response.json())
     .then(json => callback(json))
     .catch(error => console.warn("error"));
   }

  renderRow(rowData, sectionID, rowID) {
    console.warn("renderRow");
    return (
        <TouchableHighlight underlayColor='#dddddd' style={{height:44}}>
         <View>
         <Text style={{fontSize: 20, color: '#000000'}} numberOfLines={1}>{rowData.display_string}</Text>
         <Text style={{fontSize: 20, color: '#000000'}} numberOfLines={1}>test</Text>
         <View style={{height: 1, backgroundColor: '#dddddd'}}/>
        </View>
    </TouchableHighlight>
);
}

   render() {
   console.warn("render" + this.state.isLoading);
   var currentView = (this.state.isLoading) ? <View style={{height: 110, backgroundColor: '#dddddd'}} /> : <ListView dataSource={this.state.dataSource} renderRow={this.renderRow.bind(this)} enableEmptySections={true}/>
   return(
     <View>
       {currentView}
     </View>
   );
  }
}

// App registration and rendering
AppRegistry.registerComponent('AwesomeProject', () => ListViewDemo);

我在这里看到一些错误。

In your componentDidMount,你正在设置datasource代替dataSource:

  componentDidMount() {
    console.warn("componentDidMount");
    this.getTheData(function(json){
     productArray = json;
      console.warn(productArray);
     this.setState = ({
       //datasource:this.state.dataSource.cloneWithRows(productArray),
       dataSource:this.state.dataSource.cloneWithRows(productArray),
       isLoading:false
     })
    }.bind(this));   
     console.warn("component ->  " + this.state.isLoading);
  }

这就是为什么你无法渲染,因为dataSource从未有人居住。这只是一个小拼写错误。

您可能不会在 getTheData 方法中进入第二个 then ,因为您没有返回 Promise:

getTheData(callback) {
  console.warn("callback");
  var url = "https://raw.githubusercontent.com/darkarmyIN/React-Native-DynamicListView/master/appledata.json";
  fetch(url)
     //.then(response => response.json())
     .then(response => return response.json())
     .then(json => callback(json))
     .catch(error => console.warn("error"));
   }

你犯了一个错误setState,您正在分配它而不是调用它:

 //this.setState = ({
 //  datasource:this.state.dataSource.cloneWithRows(productArray),
 //  isLoading:false
 //})

 this.setState({
   dataSource:this.state.dataSource.cloneWithRows(productArray),
   isLoading:false
 })

让我知道它是否有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有 Json Api 的 ReactNative ListView 的相关文章

随机推荐

  • 如何在 Python 中向旧的 CSV 文件追加新行?

    我正在尝试向旧的 CSV 文件添加新行 基本上 每次运行 Python 脚本时它都会更新 现在 我将旧的 CSV 行值存储在列表中 然后删除 CSV 文件并使用新的列表值再次创建它 我想知道是否有更好的方法可以做到这一点 with open
  • 如何列出 FTP 连接的目录内容

    我找不到这方面的教程 在 VB NET 中我想要执行如下命令 Dim array1 as string ListFilesInFolder www example com images 我知道这可能不会那么简单 但是有人可以给我指点教程或其
  • MongoDB C# 驱动程序覆盖字符串的默认值从 null 到 string.empty

    使用 10gen mondgo db c 驱动程序 我有以下课程 BsonId public ObjectId Id get set public int AttemptId get set public int UserId get se
  • Json 下拉列表

    当我点击部门安装主题时 当我点击主题时要安装的服务 但当我点击服务时却没有看到问题 我认为对json的描述不准确 你能帮我解决这个问题吗 谢谢 我的 Jquery 代码
  • ASP.NET MVC 在哪里放置自定义验证属性

    我一直在摆弄一些 ASP NET MVC3 解决方案结构 并且已经确定了由以下项目组成的设计 MyApp Web MVC3 Web Layer MyApp Data Repositories and infrastructure for m
  • 使用php将XML数据插入mysql

    代表问题的 xml 文件部分 该 xml 文件有数百条客户记录
  • 如何将事件流式传输到 BigQuery?

    我想将事件添加到 BigQuery 中 以便使用以下服务通过图表查看它们模式分析 https modeanalytics com 我不确定是否掌握了 BigQuery 的完整概念 也许我对它做出了错误的假设 但我想使用它的目的是拥有一个 某
  • C++ 中许多 for 循环的紧凑形式

    我有一段代码如下 以及数量for循环由下式确定n这是在编译时已知的 每个for循环迭代值 0 和 1 目前 我的代码如下所示 for int in 0 in lt 2 in for int in 1 0 in 1 lt 2 in 1 for
  • @Override注释错误(android首选项)

    当我尝试使用此代码在我的应用程序中启用首选项时 import android app Activity import android content SharedPreferences import android os Bundle im
  • Material-ui v1 输入焦点样式覆盖

    我试图通过类名覆盖来覆盖输入组件焦点时的样式 我已经尝试过以下方法 const style theme gt input width 20 borderRadius 4 backgroundColor white border 1px so
  • 有没有办法分离 matplotlib 图以便计算可以继续?

    在 Python 解释器中执行这些指令后 会出现一个带有绘图的窗口 from matplotlib pyplot import plot 1 2 3 show other code 不幸的是 我不知道如何继续交互地探索由show 同时程序会
  • 客户端中出现 NoRouteToHostException

    我正在开发一个涉及服务器和客户端的程序 我尝试监听一个端口 然后从客户端向该端口发送消息 然而 这样做我得到NoRouteToHostException 我制作了一个简单的客户端和服务器应用程序来测试我是否可以为给定的服务器和客户端发送和接
  • 计算每行中缺失/NaN 的数量

    我有一个包含大量行的数据集 有些值为 NaN 如下所示 In 91 df Out 91 1 3 1 1 1 1 3 1 1 1 2 3 1 1 1 1 1 NaN NaN NaN 1 3 1 1 1 1 1 1 1 1 我想计算每行中 Na
  • 如何用动画更改 UISegmentedControl 索引?

    我可以更改选定的部分UISegmentedControl by selectedSegmentIndex 但是可以用动画改变索引吗 这不可能 您必须实现自己的自定义视图或搜索允许该操作的第三方控件
  • 如何在 Raku 中终止线程、停止承诺执行

    我正在寻找等待停止 发送异常 到正在运行的承诺SIGINT 中给出的例子doc https docs perl6 org routine signal Supply sub signal退出整个流程 而不仅仅是一名工人 有人知道如何 杀死
  • “使用”PHP 打开模态窗口

    我正在用 PHP 创建一个登录系统 并试图让它变得更好一点 当您注销时 您会被重定向回index php 像这样 header loaction index php logout true 这使得 url 看起来像 www mysite c
  • 如何使用 ClojureScript 和 Om 根据用户输入过滤列表?

    我刚刚开始使用 Om 一个基于 Reactjs 的 ClojureScript 库 我想根据用户输入过滤列表 以下有效 但解决方案似乎很复杂 还有更好的吗 ns om tut core require macros cljs core as
  • 如何使用 LowLevelKeyboardHook 挂钩 Win + Tab

    In a few words blocking Win up after Win Tab makes Windows think Win is still down so then pressing S with the Win key u
  • 如何读出 gtk3 对象的“空地 ID”

    在 Glade 中 可以为对象设置唯一的 ID 在代码中 我们可以通过搜索它的 glade ID 来获取指向该对象的指针gtk builder get object https developer gnome org gtk3 stable
  • 带有 Json Api 的 ReactNative ListView

    我无法在列表视图中显示 json 数据 我在 console log 中获取 json 数据 但在 listview 中获取不到 isLoading 始终为 false 我没有收到任何错误 catch error gt console wa