[英]Passing both props.children and a component as prop in React/Nextjs
User.tsx 是一种导航栏,它通过 {props.children} 传递所有子组件。 我也想将面包屑组件传递给它,但我收到一条错误消息:
JSX 元素类型“props.component”没有任何构造或调用签名
布局.tsx:
import UserShell from "./account/User
import Breadcrumb from "./navigation/Breadcrumb";
<User component={Breadcrumb}>{props.children}</User>;
用户.tsx:
type ShellProps = {
children: React.ReactNode | React.ReactNode[];
component?: React.ComponentType;
export default function UserShell(props: ShellProps) {
return (
<>
<props.component />
^^^^^^^^^^^^^^^^^^//error is here
<div>{props.children}</div>
</>
}
关于如何解决这个问题的任何线索?
import React from 'react'
type ShellProps = {
children: React.ReactNode | React.ReactNode[];
component?: React.ComponentType;
}
function UserShell(props: ShellProps) {
return (
<>
{props.component ? <props.component /> : null}
<div>{props.children}</div>
</>
}
您可能需要将组件 (Component) 大写
尝试更新到这个
布局.tsx:
import UserShell from "./account/User
import Breadcrumb from "./navigation/Breadcrumb";
<User Breadcrumb={Breadcrumb}>{props.children}</User>; //use Breadcrumb or Component (capital C)
用户.tsx
type ShellProps = {
children: React.ReactNode | React.ReactNode[];
Breadcrumb?: React.ElementType; //covers all elements
export default function UserShell({Breadcrumb, children}) {
return (
<>
<Breadcrumb/>
<div>{children}</div>
</>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.