Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add prop base on condition to react component?

<MyComponent something={somethingIsTrue} />

How to add something as prop to MyComponent if somethingIsTrue? I still wouldn't want something=undefined or something=false because it's still been added to MyComponent. I want it to be

not added as prop

if somethingIsTrue is falsely.

like image 217
Alicia Y Avatar asked Apr 17 '26 22:04

Alicia Y


1 Answers

I have yet to try this but something like this should do the trick.

const conditionalProps = shouldHaveProps ? {something: somethingIsTrue} : {};

<MyComponent {...conditionalProps} />
like image 192
Rajesh Paudel Avatar answered Apr 19 '26 10:04

Rajesh Paudel



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!