Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-closing element conditional element using only one line?

I would like to render a component out of two depending on a flag value. I was wondering if there is a way to do this in only one line?

I was thinking something like this: <(flag ? Comp1 : Comp2) att1={attValue} ... />

If there is not a way, is it possible to somehow add it as a feature in the future?

like image 251
Mahmoud Mousa Hamad Avatar asked Aug 16 '26 13:08

Mahmoud Mousa Hamad


1 Answers

You can interpolate a React.createElement that alternates between the two components.

<>
  {
    React.createElement(flag? Comp1 : Comp2, { att1: attValue }, null)
  }
</>
like image 86
CertainPerformance Avatar answered Aug 19 '26 02:08

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!