嗨,我正在尝试从理智CMS中获取数据,然后用道具把它传递给我的子组件。但这不管用。同样的事情和代码在我的另一个屏幕上工作,但这里我得到了一个错误。数据正在被获取,我检查了console.log,但没有被传递。我想错误就在这里,
{resturants?.map((type) => {
return (
<ResturantCard
key={type._id}
id={type._id}
imgUrl="https://links.papareact.com/gn7"
title={type.name}
genre={type.genre}
address={type._address}
description={type.description}
lat={type.lat}
long={type.long}
dishes={type.dishes}
/>
);
})}
完整的组件代码如下面所示
import { View, Text, ScrollView } from "react-native";
import { React, useEffect, useState } from "react";
import { ArrowRightIcon } from "react-native-heroicons/outline";
import ResturantCard from "./ResturantCard";
import client from "../sanity";
export default function FeaturedRow(props) {
const [resturants, setResturants] = useState([]);
useEffect(() => {
console.log(props.id);
client
.fetch(
`*[_type == "featured" && _id== $id ]{...,type[]->{
...,dishes[]->, type->{name}
},
}[0]`,
{ id: props.id }
)
.then((data) => setResturants(data), console.log("date recieved"))
.catch(console.log("No data received"));
}, []);
console.log(resturants);
//console.log(props);
return (
<View>
<View className="flex-row mt-4 items-center justify-between px-4">
<Text className="font-bold text-lg">{props.title}</Text>
<ArrowRightIcon />
</View>
<Text className="text-xs px-4 text-gray-500">{props.description}</Text>
<ScrollView
horizontal
contentContainerStyle={{
paddingHorizontal: 15,
paddingBottom: 15,
}}
className="pt-4 pb-2"
>
{/* Resturant Card*/}
{resturants?.map((type) => {
return (
<ResturantCard
key={type._id}
id={type._id}
imgUrl="https://links.papareact.com/gn7"
title={type.name}
genre={type.genre}
address={type._address}
description={type.description}
lat={type.lat}
long={type.long}
dishes={type.dishes}
/>
);
})}
<ResturantCard
id="d1"
imgUrl="https://links.papareact.com/gn7"
title="Ramzan Shinwari"
genre="Dessi Food"
address="123 Chaklala"
description="The Best Food in the Town"
lat="121343"
long="453233"
dishes={[]}
/>
<ResturantCard
id="d1"
imgUrl="https://links.papareact.com/gn7"
title="Ramzan Shinwari"
genre="Dessi Food"
address="123 Chaklala"
description="The Best Food in the Town"
lat="121343"
long="453233"
dishes={[]}
/>
<ResturantCard
id="d1"
imgUrl="https://links.papareact.com/gn7"
title="Ramzan Shinwari"
genre="Dessi Food"
address="123 Chaklala"
description="The Best Food in the Town"
lat="121343"
long="453233"
dishes={[]}
/>
</ScrollView>
</View>
);
}
子组件代码
import { View, Text, TouchableOpacity, Image } from "react-native";
import React from "react";
import { MapPinIcon, StarIcon } from "react-native-heroicons/outline";
export default function ResturantCard(props) {
return (
<TouchableOpacity className="bg-white shadow mr-4">
<Image source={{ uri: props.imgUrl }} className="h-36 w-64" />
<View className="px-3 pb-3">
<Text className="text-lg font-bold pt-2">{props.title}</Text>
<View className="flex-row items-center space-x-1">
<StarIcon color="green" size={22} opacity={0.5} />
<Text>
{props.rating} . {props.genre}
</Text>
</View>
<View className="flex-row items-center space-x-1">
<MapPinIcon />
<Text className="text-xs text-gray-500">Near . {props.address}</Text>
</View>
</View>
</TouchableOpacity>
);
}
输出控制台输出
发布于 2022-09-03 14:28:23
这里的问题是,在您设置服务器响应之后,restaurants
不再是一个数组。它是一个物体。对象不定义函数map
。
您可以在日志消息中看到这一点,即restaurants
最初是一个空数组,但是在调用setRestaurants(data)
之后,它就是一个对象。
从对象本身和在render函数中访问的属性判断,实际上您希望访问对象data
的data
属性。服务器响应的type
是一个实际的数组。这个数组中的每个对象都定义了您要访问的属性,即名称、类型、菜品.
因此,您可以将代码更改为:
then((data) => setResturants(data.type))
发布于 2022-09-03 06:20:41
只有当resturants
变量未定义时才会发生这些错误。
如果我建议您只在定义数据时设置resturants变量。
if(data) {
setResturants(data)
}
https://stackoverflow.com/questions/73591038
复制