Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

reactjs - JSX.Element' is not assignable to type 'FunctionComponent<{}>'

I am starting with React and Typescript and my code editor is claiming about my Button component (error bellow), but to eslint seems to be fine.

import React from 'react'

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

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

export default Button

The error:

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) => 

What is wrong on my code?

question from:https://stackoverflow.com/questions/65870799/jsx-element-is-not-assignable-to-type-functioncomponent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found the problem, shoud be:

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

I was forgetting the Props on React.FunctionComponent


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...