基本上,我想将injectComponentProps: object
行动态绑定(bind)到任何injectComponent的prop接口(interface)。在这种情况下,它将是injectComponentProps: InjectedComponentProps
,但我希望它是动态的。
即,一旦将prop injectComponent设置为,然后将injectComponentProps定义为任何injectComponent的组件props。
这可能吗?
interface InjectedComponentProps {
variant: string
size: number
}
const InjectedComponent: React.FC<InjectedComponentProps> = (props) => {
return <Text {...props}>hello</Text>
}
interface ComponentProps {
injectComponent: React.ReactType
injectComponentProps: object
}
const Component: React.FC<ComponentProps> = (props) => {
const InjectedComponent = props.injectComponent
return (
<>
<InjectedComponent {...props.injectComponentProps}/>
</>
)
}
const Main: React.FC = () => {
return (
<Component
injectComponent={InjectedComponent}
injectComponentProps={{ variant: 'footnote', size: 14 }}
/>
)
}
最佳答案
interface InjectedComponentProps {
variant: string
size: number
}
const InjectedComponent: React.FC<InjectedComponentProps> = (props) => {
return null
}
interface ComponentProps<T> {
injectComponent: React.FC<T>
injectComponentProps: T
}
const Component = <T extends {}>(props: ComponentProps<T>): JSX.Element => {
const InjectedComponent = props.injectComponent
return (
<>
<InjectedComponent {...props.injectComponentProps}/>
</>
)
}
const Main: React.FC = () => {
return (
<Component
injectComponent={InjectedComponent}
injectComponentProps={{ variant: 'footnote', size: 14 }}
/>
)
}
关于javascript - 在React Typescript中是否可以将组件prop接口(interface)与另一个prop一起使用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57515055/