I have a link in jsx. I may create new component for the link in the future but for now I wanted to use simple jsx. I also have a boolean prop that is telling me whether I should render the link or not. If not I want to render plain text in a span
or div
.
This is how I do it now:
{isPassed && <a href={'/uri'}>
<span>Import</span>
</a>}
{!isPassed && <span>Import</span>}
This is how I can probably do it better:
{isPassed && <a href={'/uri'}>
<span>Import</span>
</a> || <span>Import</span>}
What is the best practice handling conditional rendering like this? I want to make it more readable. First example duplicates the condition and in second example it is not apparent what is going to be rendered.
You can as well try using ternary operator, we feel it's very readable for us.
But in case of multiple conditions, then as well write a renderComponent method, like:
Further if you have complex components using props, then use the render component with more options: