gpt4 book ai didi

reactjs - JSX.Element' 不可分配给类型 'FunctionComponent<{}>'

转载 作者:行者123 更新时间:2023-12-02 18:58:39 30 4
gpt4 key购买 nike

我从 React 和 Typescript 开始,我的代码编辑器声称我的 Button 组件(错误如下),但对于 eslint 似乎没问题。

import React from 'react'

interface Props {
children: React.ReactChild | React.ReactChildren
}

const Button: React.FunctionComponent = ({ children }: Props) => {
return <button>{children}</button>
}

export default Button

错误:

Type '({ children }: Props) => JSX.Element' is not assignable to type 'FunctionComponent<{}>'.
Types of parameters '__0' and 'props' are incompatible.
Type '{ children?: ReactNode; }' is not assignable to type 'Props'.
Types of property 'children' are incompatible.
Type 'ReactNode' is not assignable to type 'string | number | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<...>)> | ReactChildren'.
Type 'undefined' is not assignable to type 'string | number | ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) =>

我的代码有什么问题?

最佳答案

发现问题,应该是:

const Button: React.FunctionComponent<Props> = ({ children }: Props) => (
<button>{children}</button>
)

我忘记了 React.FunctionComponent 上的 Props

关于reactjs - JSX.Element' 不可分配给类型 'FunctionComponent<{}>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65870799/

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