【发布时间】:2020-03-22 01:28:47
【问题描述】:
是否可以从也作为道具传递的未知组件推断道具的正确类型?
如果是已知组件(存在于当前文件中),我可以获得道具:
type ButtonProps = React.ComponentProps<typeof Button>;
但是,如果我想创建一个通用组件Box,它接受as 属性中的组件和props 属性中的组件属性。组件可以添加一些默认的props,有一些行为,没关系。基本上它类似于高阶组件,但它是动态的。
import React from "react";
export interface BoxProps<TComponent> {
as?: TComponent;
props?: SomehowInfer<TComponent>; // is it possible?
}
export function Box({ as: Component, props }: BoxProps) {
// Note: it doesn't have to be typed within the Box (I can pass anything, I can control it)
return <Component className="box" title="This is Box!" {...props} />;
}
function MyButton(props: {onClick: () => void}) {
return <button className="my-button" {...props} />;
}
// usage:
function Example() {
// I want here the props to be typed based on what I pass to as. Without using typeof or explicitly passing the generic type.
return (
<div>
<Box
as={MyButton}
props={{
onClick: () => {
console.log("clicked");
}
}}
>
Click me.
</Box>
</div>
);
}
要求:
- 必须在不传递泛型类型的情况下工作(可能吗?),因为它几乎可以在任何地方使用
- 必须使用用户定义的组件 (
React.ComponentType<Props>) - 如果它也适用于 react html 元素(a、按钮、链接......它们有不同的道具)会很棒,但不是必需的
【问题讨论】:
-
我正在努力用 forwardRef 做类似的事情
标签: reactjs typescript