TransWikia.com

is it possible to use inline if with react components?

Stack Overflow Asked by handsome on December 22, 2021

I recently learned about this. (what´s the name of it?)

condition && console.log("hello")

how can I use it with a component? like

condition && <MyComponent />

that´s it. thank you!

2 Answers

It's possible to use inline for only true conditions:

{condition && <MyComponent />}

And for true and false conditions:

{condition ? <MyComponent /> : <MyFalseComponent />}

Answered by Nicolas Acosta on December 22, 2021

you can use it on component like this,

{condition && <MyComponent />}

See https://reactjs.org/docs/react-component.html#render this line in types

Booleans or null. Render nothing. (Mostly exists to support return test && pattern, where test is boolean.)

Answered by aeXuser264 on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP