繁体   English   中英

“ReactElement”类型中缺少属性“...”<any, any> ' 但在类型 '{...}' 中是必需的</any,>

[英]Property '...' is missing in type 'ReactElement<any, any>' but required in type '{...}'

我是 TypeScript 的新手,我正在尝试通过使用以下代码从getStaticProps获取数据来呈现页面:

import React, {FormEvent, useState} from "react";
import { InferGetStaticPropsType } from "next";
import AddPost from '../components/AddPost';
import Post from "../components/Post";
import { IPost } from "../types";

const API_URL: string = 'https://jsonplaceholder.typicode.com/posts'

export default function IndexPage ( {
    posts
}) : InferGetStaticPropsType<typeof getStaticProps> {
    const [postList, setpostList] = useState(posts);

    if(!postList) {
        return <h1>Loadin....</h1>
    } 

    return (
        <div>
            <h1>My posts</h1>
            {postList.map((post : IPost) => {
                <div>
                    <h1>post{post.id}</h1>
                    <Post key={post.id} post={post}
                </div>
            })}
        </div>
    )

}

export async function getStaticProps() {
    const res = await fetch(API_URL)
    const posts : IPost[] = await res.json()

    return {
        props: {
            posts
        }
    }
}

我的帖子类型定义为:

export interface IPost {
    id: number
    title: string
    body: string
  }

代码执行良好,但在尝试返回 jsx 以在屏幕上呈现时出现 ts 错误:

Property 'posts' is missing in type 'ReactElement<any, any>' but required in type '{ posts: IPost[]; }'.

你能帮我解决这里有什么问题吗?

您似乎输入错误。 我建议您使用 React 中的FC键入IndexPage ,因为该页面是一个 React 功能组件,如下所示:

import { FC } from "react";
const IndexPage: FC<{ posts: IPost[] | null }> = ({ posts }) => {
  return <div></div>;
};
export default IndexPage;

此外,您的map部分也不正确,因为您没有返回任何内容,也没有添加key

{postList.map((post: IPost) => {
  return (
    <div key={post.id}>
      <h1>post{post.id}</h1>
      <Post key={post.id} post={post}
    </div>
  );
})}

输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,>

[英]Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“ any []”中缺少属性“ 0”,但是类型“ [{{id:string; gp:布尔值; }] '{ rating: any; 类型中缺少属性 'onClick' }' 但在“RatingType”类型中是必需的 Typescript:类型“Element[]”不可分配给类型“ReactElement” <any, string | jsxelementconstructor<any> >'</any,> 输入'元素| undefined' 不可分配给类型 'ReactElement <any, string | ((props: any)< div><div id="text_translate"><p> 我有一个看起来像这样的组件。 这个版本完美运行:</p><pre> export default function StatusMessage(isAdded: boolean, errorMessage: string) { if (isAdded) { return <ResultAlert severity="success" text="User Added" />; } else { if (errorMessage.== '') { if ( errorMessage;includes('email') ) { return <ResultAlert severity="error" />. } if ( errorMessage;includes('phone number') ) { return ( <ResultAlert severity="error" text="" /> ); } } } }</pre><p> 现在,我正试图改变我导出它的方式。 我正在尝试这个:</p><pre> type StatusMessageProps = { isAdded: boolean; errorMessage: string; } export const StatusMessage: React.FunctionComponent<StatusMessageProps> = ({ isAdded, errorMessage }) => {</pre><p> 但我不断收到错误消息:</p><pre> Type '({ isAdded, errorMessage }: PropsWithChildren<StatusMessageProps>) => Element | undefined' is not assignable to type 'FunctionComponent<StatusMessageProps>'. Type 'Element | undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'. Type 'undefined' is not assignable to type 'ReactElement<any, string | ((props: any) => ReactElement<any, string |... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | null'.</pre><pre> I am using the same method on different pages but the error is only here</pre><p> 编辑:</p><p> 我像这样使用这个组件:</p><pre> const [isAdded, setIsAdded] = useState(false); {isSubmitted && StatusMessage(isAdded, errorMessage)}</pre><p> 它在 isAdded 上给我一个错误</p><pre>Argument of type 'boolean' is not assignable to parameter of type 'PropsWithChildren<StatusMessageProps>'.ts(2345)</pre></div></any,> “Element”类型的参数不可分配给“ReactElement”类型的参数<any></any> 类型“ any []”的参数不能分配给类型“ A”的参数。 类型“ any []”中缺少属性“ a” 类型中缺少属性,但类型中需要属性 JSX 元素类型 &#39;ReactElement<any> 不是 JSX 元素的构造函数。 类型 &#39;undefined&#39; 不能分配给类型 &#39;Element | 空值&#39; “组件”不能用作 JSX 组件。 它的元素类型&#39;ReactElement<any, any> | Component&lt;{}, any, any&gt;&#39; 不是有效的 JSX 元素 类型“{}”中缺少属性“submitAction”,但在类型中是必需的
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM