【发布时间】:2022-01-01 18:37:39
【问题描述】:
我正在制作一个从 API 获取数据并将其显示在屏幕上的应用程序。渲染屏幕时,会出现以下错误:渲染错误未定义不是对象(正在评估“this.text”)。有趣的是,除了节点模块中的文件之外,我的任何屏幕中都没有像“this.text”这样的东西。无论如何,这是文件
呈现数据的屏幕:
/* the screen that renders the data */
import React, {useEffect, useState} from 'react';
import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native';
const Experts = () => {
useEffect(()=> {
fetch('http://icantputtheactualurlcuzitsmyipaddress.com/expertsList/',{
method: 'GET'
})
.then(resp => resp.json)
.then(data => {
setData(data)
})
.catch(error => console.log('something is not suppose to be happening'))
}, [])
const [data, setData] = useState([]);
return (
<SafeAreaView style={styles.container}>
<FlatList
data={data}
renderItem={({ item }) => (
<Text style={styles.item}>{item.name}</Text>
)}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
item: {
backgroundColor: 'teal',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
color: 'white',
},
title: {
fontSize: 32,
},
});
export default Experts;
我真的很感谢任何输入,我对此有点陌生。
【问题讨论】:
标签: javascript react-native rest