gpt4 book ai didi

reactjs - 将扩展的 NextPage 类型分配给页面组件时出现类型错误

转载 作者:行者123 更新时间:2023-12-05 03:21:23 26 4
gpt4 key购买 nike

我在 const Page 上遇到类型错误,如下面的屏幕截图和代码块所示。

enter image description here

Type '{ (props: Props): JSX.Element; getLayout(page: ReactElement<any, string | JSXElementConstructor<any>>): JSX.Element; }' is not assignable to type 'NextPageWithLayout'.
Type '{ (props: Props): JSX.Element; getLayout(page: ReactElement<any, string | JSXElementConstructor<any>>): JSX.Element; }' is not assignable to type 'FunctionComponent<{}> & { getInitialProps?(context: NextPageContext): {} | Promise<{}>; } & { getLayout?: ((page: ReactElement<any, string | JSXElementConstructor<...>>) => ReactNode) | undefined; }'.
Type '{ (props: Props): JSX.Element; getLayout(page: ReactElement<any, string | JSXElementConstructor<any>>): JSX.Element; }' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters 'props' and 'props' are incompatible.
Property 'pictures' is missing in type '{}' but required in type 'Props'.ts(2322)
galleryssr.tsx(19, 3): 'pictures' is declared here.

错误是说 NextPageWithLayout 不接受 getLayout,但在我引入使用 getServerSideProps 获取数据并传递 props 之前它工作正常in 作为参数。

我想我需要以某种方式更新 getLayoutNextPageWithLayout 以接受 Prop ,但我无法弄清楚语法。

NextLayoutWithPage_app.tsx 中定义:

export type NextPageWithLayout = NextPage & {
getLayout?: (page: ReactElement) => ReactNode;
};

type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};

function MyApp({ Component, pageProps }: AppPropsWithLayout) {
const getLayout = Component.getLayout ?? ((page) => page);

return (
<ThemeContextProvider>
{getLayout(<Component {...pageProps} />)}
</ThemeContextProvider>
);
}

export default MyApp;

galleryssr.tsx

import type { ReactElement } from "react";
import { SidebarLayout } from "../layouts/SidebarLayout";
import type { NextPageWithLayout } from "./_app";
import Image from "next/image";
import { GetServerSideProps } from "next";

type Picture = {
id: string;
author: string;
width: number;
height: number;
url: string;
download_url: string;
};

// Define type animals which is an array of objects
type Props = {
pictures: Picture[];
};

const Page: NextPageWithLayout = (props: Props) => {
return (
<div className="w-full text-left">
<h1>This is a server side rendered gallery</h1>
<div className="grid grid-cols-1 grid-gap-4">
{props.pictures.map((picture) => {
return (
<div
className="flex flex-col items-center justify-center bg-base-200 p-4 rounded-md my-4"
key={picture.id}
>
<div className="h-[300px] w-[300px] relative">
<Image
src={picture.download_url}
alt={picture.id}
layout="fill"
objectFit="contain"
width={picture.width / 20}
height={picture.height / 20}
/>
</div>

<div>{picture.author}</div>
</div>
);
})}
</div>
</div>
);
};

Page.getLayout = function getLayout(page: ReactElement) {
return <SidebarLayout>{page}</SidebarLayout>;
};

// This gets called on every request
export const getServerSideProps: GetServerSideProps = async () => {
// Fetch data from external API
const res = await fetch(`https://picsum.photos/v2/list`);
const pictures = await res.json();
return { props: { pictures } };
};

export default Page;

最佳答案

为了使 NextPageWithLayout 也接受组件的 Prop 类型,您可以定义一个可选的通用类型,您将传递给 NextPage

export type NextPageWithLayout<P = {}> = NextPage<P> & {
getLayout?: (page: ReactElement) => ReactNode;
};

您现在可以按如下方式定义您的页面的类型:

type Props = {
pictures: Picture[];
};

const Page: NextPageWithLayout<Props> = (props) => {
// Your component's code
};

关于reactjs - 将扩展的 NextPage 类型分配给页面组件时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73002071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com