TIL #2: How to Conditionally Add Props to a React Component?

TIL #2: How to Conditionally Add Props to a React Component?

Using Inline Ternary Operator

Conditionally Spreading the Object Properties

We can use ternary or logical && operator, along with the spread operator, to conditionally add props to a component like so:

<Tooltip
       message={message}
       // using the ternary operator
       {...(!isMobile ? { position: 'left' } : {})}
       // using the logical && operator
       {...(!isMobile && { position: 'left' })}
 >
       <InfoIcon />
</Tooltip>