Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better jsx condition to order component

I have an icon position prop that decide whether it's placed on the left or on the right of the children. I have this working

<List>
    {iconPosition === 'right' && (
        <Text />
    )}
    {icon && (
        <Icon />
    )}
    {iconPosition === 'left' && (
        <Text />
    )}
</List>

But I think it can be more simple, although in my opinion my above code is readable.

like image 264
Melissa94 Avatar asked Nov 26 '25 11:11

Melissa94


1 Answers

You can't do this any other way. You're using individual conditions with their own result.

Maybe you could place them on one line to make them more readable.

<List>
    {iconPosition === 'right' && <Text />}
    {icon && <Icon />}
    {iconPosition === 'left' && <Text />}
</List>

or

render() {
  const textLeft = (iconPosition === 'left');
  const textRight = (iconPosition === 'right');

  ...
}

And use that. But that's mostly down to your preference / the coding style you and your colleagues are using..

like image 166
Alserda Avatar answered Nov 29 '25 01:11

Alserda



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!