react-native - React Native FlatList 不显示任何内容
问题描述
我正在制作我的第一个 react native 的平面列表:
<FlatList
data ={this.state.courselist}
renderItem={ this.item}
keyExtractor={(item)=>item.title}
>
</FlatList>
this.state.courselist 是这样定义的:
courselist:{
progress:[],
title:[]
},
并使用这些函数动态填写:
async getdata(){
var id = await SecureStore.getItemAsync("id");
console.log(id)
return fetch('http://192.168.2.128:4000/StuCurs/'+id)
.then(response => response.json())
.then(responseJson => {
responseJson.forEach(element =>{
console.log(element);
this.getcoursedata(element.cursusId,element.progression)
this.state.courselist.progress.push(
Number( parseInt(element.progression)))
console.log("ppp-"+this.state.courselist.progress[0])
this.setState({ Render: "" });
})
})
.catch(error => {
console.log(error)
})
}
async getcoursedata(id,progress){
return await fetch("http://192.168.2.128:4000/Cursuss/"+id)
.then(response => response.json())
.then(responseJSON=>{
console.log(responseJSON.naam)
this.state.courselist.title.push(responseJSON.naam.toString())
console.log(this.state.courselist.title[0])
this.setState({ Render: "" });
})
}
该列表没有显示任何内容。我最好的猜测是我的课程表结构是错误的,考虑到我必须如何从数据库中获取数据,我不确定如何对其进行重组以使其按预期工作。
但是,我创建了一个测试变量来测试我是否可以呈现任何预制数据:
testarray:[
{
title:"wiskunde",
progress:25
},
{
title:'nederlands',
progress:55
}
],
如果我尝试使用这个数组,页面根本不会加载。如何修复我的页面?
解决方案
如果您查看文档,您会发现renderItem
prop 需要传递一个函数,每个项目都映射到该函数,该函数返回一些 JSX。这可能很简单:
const renderItem = ({ item }) => <Text>{item.title} {item.progress}</Text>
推荐阅读
- angular - 使用 Angular 6 设置输入字段的值
- laravel - 试图从数据库返回视图但得到 -“未定义的变量错误 - Laravel 5.2”
- javascript - SmartAdmin 模板页面功能运行两次 Bug
- android - 更改后端密码时注销用户
- angular - 如何在 Ionic 中为 chart.js 启用水平滚动
- javascript - 如何在使用 Google protobuf 时调用困在“System.register()”模块中的 javascript 函数(从 typescript 生成)?
- java - Selenium WebDriver 中的 driver.switchTo().parentFrame() 和 driver.switchTo().defaultContent() 方法有什么区别?
- webdriver - Appium 创建驱动程序非常缓慢。(Inspector 加载元素树的速度非常慢。)
- javascript - Angular Material:单击 mat-sidenav 中的项目应更改 mat-sidenav-content 中的内容
- c++ - 如何将字符串转换为目录?在 Linux 上提升