handsome
handsome

Reputation: 2422

is it possible to use inline if with react components?

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!

Upvotes: 2

Views: 127

Answers (2)

Nicolas Acosta
Nicolas Acosta

Reputation: 807

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

{condition && <MyComponent />}

And for true and false conditions:

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

Upvotes: 2

ezio4df
ezio4df

Reputation: 4175

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.)

Upvotes: 5

Related Questions