【问题标题】:(parameter) children: ReactNode Object is possibly 'null' or 'undefined'(参数) children: ReactNode 对象可能是 'null' 或 'undefined'
【发布时间】:2022-01-21 12:18:05
【问题描述】:

1-(参数)children:ReactNode 对象可能是“空”或“未定义”。

2-属性“类型”在类型“布尔”上不存在 |反应儿童 |反应片段 |反应门户'。 类型“字符串”上不存在属性“类型”。

import Head from "next/head"

interface AppProps{
  children: React.ReactNode
}
const Layout = ({ children }: AppProps) => {
  return (
    <>
       <Head>
       <title>{children.type.name}</title>
       </Head>
   </>
  )
}
export default Layout

【问题讨论】:

    标签: reactjs typescript next.js react-typescript


    【解决方案1】:

    类型ReactNode 是联合类型boolean | React.ReactChild | React.ReactFragment | React.ReactPortal | null | undefined。如您所见,这包括nullundefined,因此您必须处理代码中children 将是nullundefined 的可能性。

    如何处理取决于您自己。您无法在 Layout 中呈现任何内容,或者引发错误,或者呈现一些占位符文本而不是 children.type.name

    【讨论】:

      猜你喜欢
      • 2021-02-03
      • 2021-10-25
      • 1970-01-01
      • 2022-11-24
      • 2020-08-25
      • 2022-09-27
      • 2018-01-01
      • 1970-01-01
      • 2021-09-10
      相关资源
      最近更新 更多