[英]Extending the default behaviour of React.FC in Nextjs
所以我有一个在 Nextjs 中使用以下代码的教程,我计划将它从 javascript 移动到 typescript。 我是 typescript 的新手,所以我已经尝试了我见过的大部分文章,但扩展默认的 React.FC 似乎不起作用。
function Explore() {
return <p className="p-4">Explore</p>;
}
Explore.headerTitle = "Search";
export default Explore;
我的组件
const Home: React.FunctionComponent = () => {
return (
<div>
<Head>
<title>Home / Twitter</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<div>
<p>Hello </p>
</div>
</div>
);
};
Home.headerTitle = "Search";
我的界面
export interface HeaderTitle extends React.FunctionComponent {
headerTitle: string;
}
我想向我的功能组件添加一个方法。
尝试将您的接口与React.FunctionComponent
分开声明(为了可重用性):
export interface WithHeaderTitle {
static headerTitle?: string;
}
然后在您的组件代码中,导入接口并像这样使用它:
const Home: React.FunctionComponent & WithHeaderTitle = () => { ... }
这里的关键字是“交叉点类型”。 您可以在这里阅读更多相关信息: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.