[英]How to add stencil's IntrinsicElements to react's IntrinsicElements
我计划使用 0.0.0-experimental 版本在 React App 中使用一些模板组件,这样我就可以将我的组件与<kebab-case>
一起使用。 我是 typescript 的新手,想了解如何将模板自动生成的 IntrinsicElements 添加到我的 React IntrinsicElements。 我现在在我的 React 组件中所做的是:
import React from "react";
import { JSX as stencils } from "stencil-project/dist/types/components"; // (I am using a yarn mono-repo)
import { defineCustomElements } from 'stencil-project/dist/esm/loader';
defineCustomElements();
declare global {
namespace JSX {
interface IntrinsicElements extends stencils.IntrinsicElements {}
}
};
export const App = () => {
return (<my-component></my-component>);
}
它有效,但感觉它不是正确的方法。 你能给我指点一些文档或其他有用的资源吗? https://stenciljs.com/docs/typed-components告诉我我可以做到,但没有告诉我怎么做。
您必须在InstrictElements
界面中添加组件,如下所示:
// Some global.d.ts file.
// Importing React namespace is important
import React from 'react';
interface MyStencilComponentProp {
myProp: number;
}
declare global {
namespace JSX {
interface IntrinsicElements {
'my-stencil-comp': MyStencilComponentProp;
}
}
}
然后在你的反应组件中,你可以像其他原生 HTML 元素一样使用:
export function ReactComp(props: any) {
return (
<>
<my-stencil-comp myProp={10} />
</>
);
};
使用 TypeScript 的声明合并功能, @types/react
定义的IntrinsicElement
接口和您的d.ts
文件将合并为一个接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.