1

In a React project, I have Itinerary.tsx, ItineraryDetails.tsx, Description.tsx, Weather.tsx and WeatherDetails.tsx components.

Inside Itinerary.tsx I have something like this:

<ItineraryDetails>
 <Description/>
</ItineraryDetails>

Inside Weather.tsx I have something like this:

<WeatherDetails>
 <Description/>
</WeatherDetails>

Inside Description.tsx I have several other elements and a link before a badge:

      <Link>
      </Link>
      <Badge>
      </Badge>

Since everything besides the link is the same for both Itinerary.tsx and Weather.tsx, can I use Description.tsx for both and somehow conditionally render the link only when inside Itinerary.tsx? Or is it better to create 2 "Descriptions"? I'm thinking that having 2 components so similar is a waste and it's not practical once you need to do changes.

I can't just add the link after ItineraryDetails or before Description because of the html elements' order (the link should appear before the badge).

1 Answer 1

3

You could pass a property to the Description component:

{this.props.showLink && <Link></Link>}
<Badge></Badge>

And then use it like so:

<ItineraryDetails>
 <Description showLink />
</ItineraryDetails>
<WeatherDetails>
 <Description/>
</WeatherDetails>
Sign up to request clarification or add additional context in comments.

Comments

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.