reactjs - 对于 child 的 TypeScript Prop ,我应该使用什么类型?

标签 reactjs typescript

我正在做:

export default function PageTitle(props: ReactElement) {
  return (
    <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
  )
}

但我收到投诉:

Type error: Property 'children' does not exist on type 'ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)>) | (new (props: any) => Component<any, any, any>)>'

什么是正确的类型?

最佳答案

我会这样写

   type Props = {
      children: string;
    };

     const PageTitle : React.FC<Props> = (props) => {
      return (
        <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{props.children}</Typography>
      )
    }

    export default PageTitle;

或者像这样

type Props = {
  children: string;
};

 const PageTitle = ({children, ...props}: Props) => {
  return (
    <Typography variant='h3' style={{ fontWeight: 600 }} gutterBottom={true} {...props}>{children}</Typography>
  )
}

export default PageTitle;

关于reactjs - 对于 child 的 TypeScript Prop ,我应该使用什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508053/

相关文章:

css - 如何在 Material 图标和描述之间创建一些空白?

javascript - Typescript 中的类型保护正在缩小到从不键入

reactjs - 如何在 React 中引用组件的 props?

android - 意外的顶级异常( react native )

在服务器上找不到 CSS 样式表?

arrays - Angular 5 array.filter 内未定义

json - 如何使用 typescript (Angular2) 遍历 JSON 对象

angular - 无法在 TypeScript 中为 Map<TKey, TValue[]> 编写扩展方法

javascript - 如何在React中更改div的innerHtml?

javascript - 传播运算符 (...) 在 es6 中的数组中创建额外字段