在react中使用typescript,无状态组件不能分配给类型' React.SFC'

时间:2018-05-21 01:37:30

标签: reactjs typescript

TypeScript:2.8.3
@ types / react:16.3.14

当我将组件声明为GlobalMap.getOrElse(x._1, Array())(别名为JSX.Element)时,函数组件中的返回类型为React.SFC

发生了三个错误:

  1. React.StatelessComponent

  2. TS2322: Type 'Element' is not assignable to type 'StatelessComponent<{}>', Type 'Element' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any>'

  3. TS2339: Property 'propTypes' does not exist on type '(props: LayoutProps) => StatelessComponent<{}>'

  4. TS2339: Property 'defaultProps' does not exist on type '(props: LayoutProps) => StatelessComponent<{}>'

1 个答案:

答案 0 :(得分:1)

返回类型不是组件,函数本身是组件:

const LayoutHeaderItem: React.SFC<LayoutHeaderItemProps> =
    (props: LayoutHeaderItemProps) => { 
        // ... 
    }

这个问题有点老了,SFC弃用了FunctionComponent