I have this components:
export function Hover(props: PolygonProps) {
//...
}
export function Route(props: CenterProps) {
//...
}
This enum:
export enum Highlight {
HOVER,
ROUTE,
}
This object:
export const HIGHLIGHTS = {
[Highlight.HOVER]: Hover,
[Highlight.ROUTE]: Route,
};
And this code in render:
let HighlightComponent;
сonst center = props.center;
const points = props.points;
if (highlight !== undefined) {
HighlightComponent = HIGHLIGHTS[highlight] as React.ComponentType<CenterProps | PolygonProps>;
}
const centerProps: CenterProps = { center };
const polygonProps: PolygonProps = { points };
return <div>{HighlightComponent && <HighlightComponent />}</div>
Question: how to pass props of needed type (CenterProps or PolygonProps) if i dont know type? Is it correct structure for the case or not?
CenterPropsorPolygonProps. So let's say you haveconst someProps: CenterProps | PolygonProps = ..., then you can pass them like this:<HighlightComponent {...someProps} />