只是对此有点困惑:
export async function getServerSideProps() {
// Get all homes
const homes = await prisma.home.findMany();
// Pass the data to the Home page
return {
props: {
homes: JSON.parse(JSON.stringify(homes)),
},
};
}
export default function Home({ homes = [] }) {
return (
<div>
<h1 className="text-xl font-medium text-gray-800">
Top-rated places to stay
</h1>
<p className="he">
Explore some of the best places in the world
</p>
<div className="mt-8">
<div>{homes} </div>
</div>
</div>
);
它可以工作,但我认为要访问家庭,您必须执行homes.homes,因为家庭是道具内部的一个对象或属性,道具作为“家”传递给函数,那么如果道具被命名为“homes”,那么实际的homes不应该是其中的一个属性吗?因此,homes.homes能请人解释一下为什么不是这种情况吗?谢谢
发布于 2022-11-23 07:58:46
函数组件的第一个参数是整个props对象。这个道具对象是位于props:
右侧的getServerSideProps
对象。
在不进行析构的情况下,通过为props
对象在getServerSideProps
中声明一个额外的变量,可以更容易地可视化。以下代码与当前代码等效。
export async function getServerSideProps() {
// Get all homes
const homes = await prisma.home.findMany();
// Pass the data to the Home page
const props = { homes: JSON.parse(JSON.stringify(homes)) }
return {
props: props,
};
}
export default function Home(props) {
const homes = props.homes ?? [];
// ...
在getServerSideProps
中的props
参数之上是与Home
中的props
参数完全相同的对象。它有一个属性,homes
,可以被称为“道具”。该属性看起来是从JSON.parse
获取的数组。
https://stackoverflow.com/questions/74549654
复制相似问题