Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine a React element and text inside a ternary?

Normally if you have something like:

<SomeComponent foo={bar} />

you can make it optional by using a ternary:

{bar ? <SomeComponent foo={bar} /> : null}

But what if the block you are starting with contains a component plus some text (a single space) and a variable, e.g.:

<SomeComponent foo={bar} /> {foobar}

Wrapping it in parentheses won't work:

{bar ? (<SomeComponent foo={bar} /> {foobar}) : null}

and in fact the only way I found to make it work was to wrap everything in another element:

{bar ? <span><SomeComponent foo={bar} /> {foobar}</span> : null}

Is there any other way to tell React that:

<SomeComponent foo={bar} /> {foobar}

is a discrete chunk, so that I can use it in a ternary (inside JS, not JSX, logic) ... without adding meaningless wrapper elements?

like image 864
machineghost Avatar asked Jan 06 '18 22:01

machineghost


People also ask

How do I combine two components into one in React?

We can divide the UI of the application into small components and render every component individually on the web page. React allows us to render one component inside another component. It means, we can create the parent-child relationship between the 2 or more components.

What is the use of && in React?

Inline If with Logical && Operator It works because in JavaScript, true && expression always evaluates to expression , and false && expression always evaluates to false . Therefore, if the condition is true , the element right after && will appear in the output. If it is false , React will ignore and skip it.

How ternary operator works in react?

React Ternary Operator | How Ternary Operator Works in React? The ternary operator in the react js works in the same way how it works in the Javascript.

What is the ternary operator in JavaScript?

The ternary operator is a simplified conditional operator like if / else. Complete this ternary operator statement.

How to test two conditions in a single statement using ternary operator?

In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on the condition2. If condition2 is correct, then the output is Expression1.

What is the HTML part of react?

This part is the html which is responsible to contain all the react components and to display them . Below we have displayed two screens of example out of it one is the screen before clicking the button access and another is the button after clicking the access, here on clicking the button and message both are changing.


2 Answers

There used to be two suboptimal ways to achieve this:

  1. Using an array, which requires adding keys to React elements:

    {bar ? [<SomeComponent key="1" foo={bar} />, " ", foobar] : null}
    
  2. Creating a wrapper component, like @margaretkru suggested.

But with React 16.2.0+ you can use improved fragments:

{bar ? <React.Fragment><SomeComponent foo={bar} /> {foobar}</React.Fragment> : null}

or, even better, you can use the shorthand syntax:

{bar ? <><SomeComponent foo={bar} /> {foobar}</> : null}

Fragments won't produce an additional container element.

like image 160
silvenon Avatar answered Sep 22 '22 00:09

silvenon


You could define a small component that would act as a wrapper but won't render any markup, but it technically is still a wrapper. I use this approach by defining Aux component:

const Aux = (props) => (props.children);

Then you can do:

{bar ? <Aux><SomeComponent foo={bar} /> {foobar}</Aux> : null}

This at least avoids unnecessary markup in the resulting html which might be crucial for styling purposes, if you are using flex-box for example.

like image 27
margaretkru Avatar answered Sep 22 '22 00:09

margaretkru