1

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?

2
  • 1
    Where do your props come from? You know the props are CenterProps or PolygonProps. So let's say you have const someProps: CenterProps | PolygonProps = ..., then you can pass them like this: <HighlightComponent {...someProps} /> Commented May 4, 2020 at 6:33
  • @sroes, i've updated the question. Yes, but i don't want to pass props that have not to be passed, i can pass every prop, but it doesn't look clear. Commented May 4, 2020 at 6:35

2 Answers 2

1

Since you have two seperate props, you must still check to see which one to use. So I would drop the HIGHLIGHTS constant and simply use a switch:

   const centerProps: CenterProps = { center: 'someValue' };
   const polygonProps: PolygonProps = { points: 'someOtherValue' };

   let highlightComponent;
   if (highlight !== undefined) {
       switch (highlight) {
           case Highlight.HOVER:
               highlightComponent = <Hover {...polygonProps} />;
               break;
           case Highlight.ROUTE:
               highlightComponent = <Route {...centerProps} />;
               break;
       }
   }

   return <div>{highlightComponent}</div>

Another option would be to define the props like this:

const centerProps: CenterProps = { center: 'someValue' };
const polygonProps: PolygonProps = { points: 'someOtherValue' };

export const HIGHLIGHT_PROPS = {
    [Highlight.HOVER]: polygonProps,
    [Highlight.ROUTE]: centerProps,
};

And then:

let HighlightComponent;
let highlightProps;
if (highlight !== undefined) {
    HighlightComponent = HIGHLIGHTS[highlight] as React.ComponentType<CenterProps | PolygonProps>;
    highlightProps = HIGHLIGHT_PROPS[highlight] as CenterProps | PolygonProps;
}

return <div>{HighlightComponent && <HighlightComponent {...highlightProps} />}</div>
Sign up to request clarification or add additional context in comments.

2 Comments

This is pretty sad answer, because i really don't like switch-case in render.
I will use switch-case, thank you for the answer, i though there is another way to do that without switch-case, because enum values count will increase.
1

If I understood correctly you can simply do:

if (highlight !== undefined) {
    HighlightComponent = HIGHLIGHTS[highlight] === HIGHLIGHTS.HOVER 
       ? <Hover {...polygonProps}/>
       : <Route {...centerProps}/>;
}

return <div>{HighlightComponent && <HighlightComponent />}</div>

However this looks like a code smell so I would suggest that you change your props to be HighlightComponent: JSX.Element and just pass in the relevant component.

1 Comment

I have enum value (highlight) in redux store so i can't pass component with props.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.