我正在尝试在屏幕上显示一个结构数据数组。问题是我不知道如何输出具有这种结构的数组,其中每个对象都有一个数组系统。它看起来应该类似于 SectionList,但对于 header 和 item 来说,它应该是多个数据的字符串。
实现基于此示例https://reactnativedev.ru/rn/sectionlist/#_1
目前,表单崩溃并出现以下错误:
(NOBRIDGE)错误警告:TypeError:无法读取未定义的属性“length”
请求正确发送,返回数据,数据与json数据类型匹配。但也许问题也出在结构中的“子数组”系统的声明中?
在此之前我尝试通过嵌套的平面列表进行输出,但系统数据并未输出。
import { SectionList, Text, TouchableOpacity, View } from 'react-native'
import { router } from 'expo-router';
type Structure = {
id: number;
comments: number;
status: string;
systems:[
id: number,
numberII: string,
systemName: string,
],
};
const Struct = () => {
const [data, setData] = useState<Structure[]>([]);
const getStructure = async () => {
try {
const response = await fetch('https://...');
const json = await response.json();
setData(json);
console.log('ResponseSeeStructure:', response);
} catch (error) {
console.error(error);
} finally {
}
};
useEffect(() => {
getStructure();
}, []);
//const font16 = MonoSizeText(16);
return(
<SectionList
sections = {data}
keyExtractor={({id}) => id}
renderSectionHeader={({section: {comments, status}}) => (
<View style={{flexDirection: 'row', width: '100%', height: 32, paddingTop: 6, marginBottom: '3%', alignSelf: 'center', }}>
<View style={{width: '23%'}}>
<Text style={{ fontSize: 14, color: '#334155', textAlign: 'center' }}>{comments}</Text>
</View>
<View style={{width: '42%'}}>
<Text style={{ fontSize: 14, color: '#334155', textAlign: 'center' }}>{status}</Text>
</View>
</View>
)}
renderItem={({item: {systems}}) => (
<TouchableOpacity onPress={() =>{ router.push('/structures/system')}}>
<View style={{ alignSelf: 'center', backgroundColor: '#E0F2FE', flexDirection: 'row', width: '98%', height: 32, marginBottom: 41, borderRadius: 8}}>
<View style={{width: '10%', justifyContent: 'center',}}>
<Text style={{ fontSize:14, color: '#334155', textAlign: 'center' }}>{systems.numberII}</Text>
</View>
<View style={{width: '25%', justifyContent: 'center',}}>
<Text style={{ fontSize: 14, color: '#334155', textAlign: 'center' }}>{systems.systemName}</Text>
</View>
</View>
</TouchableOpacity>
)}
/>
);
};
export default Struct;```
renderItem 的文档说明:
'item' (объект) — объект элемента, указанный в ключе data этого раздела.因此,该对象必须有一个数据数组,并且不能被命名为其他任何名称。这是一种不寻常的行为,但这就是它的工作原理。
下面我添加了修改后的代码,它对我有用。如果将其重命名
data为systems,则会出现错误Cannot read property 'length' of undefined