Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple elements in JSX

I'm trying to return two links if the user is not logged in. Something like this:

<Nav>
    {if(this.state.user) {
        <NavItem onClick={this.logout}>Log out</NavItem>
    } else {
        <NavItem onClick={this.login}>Log in</NavItem>
        <NavItem onClick={this.register}>Register</NavItem>
    }
    }
</Nav>

I know I can do this with a ternary operator:

<Nav>
    {this.state.user ?
        <NavItem onClick={this.logout}>Log out</NavItem>
        :
        <NavItem onClick={this.login}>Log in</NavItem>
    }
</Nav>

The problem is I want to render the two NavItems. I saw I can do it with a function, but when I try to do it in a function like this:

myFunction(){
    return(
        <NavItem onClick={this.login}>Zaloguj się</NavItem>
        <NavItem onClick={this.login}>Zaloguj się</NavItem>
    )
}

It tells me that the second element is unreachable and breaks. So how can I return two elements? Stringifying the code doesn't help

like image 647
Alex Ironside Avatar asked May 28 '18 11:05

Alex Ironside


People also ask

How do I return multiple elements in React?

Use a React fragment to return multiple elements from a component, e.g. <><div>First</div><div>Second</div></> . React Fragments are used when we need to group a list of children without adding extra nodes to the DOM. Copied! We used a React fragment to group a list of children without adding extra nodes to the DOM.

Which function can help in returning multiple components within JSX?

In react 16, there is functionality to return two or more element using array in render() function. In Figure 1.1, shows the react component, where multiple DOM element is wrapped in one root DOM element.

Can you have multiple returns in React?

You can have multiple return statements, but only the first one that is encountered by the logic, will run. The if/else block is not required.


2 Answers

If you are using React v16.2.0 and above you can use the shorter version of Fragments

<>
 <NavItem onClick={this.login}>Zaloguj się</NavItem>
 <NavItem onClick={this.login}>Zaloguj się</NavItem>
</>

If you are using a version below React v16 you can only return one element in jsx, so you have to wrap your elements inside one:

<div>
 <NavItem onClick={this.login}>Zaloguj się</NavItem>
 <NavItem onClick={this.login}>Zaloguj się</NavItem>
</div>

If you are using React v16 and abose you can use Fragments

import React, { Fragment } from 'react';

...
...

    <Fragment>
     <NavItem onClick={this.login}>Zaloguj się</NavItem>
     <NavItem onClick={this.login}>Zaloguj się</NavItem>
    <Fragment/>

You could also return an Array of Elements as announced here:

 return [
    <NavItem key="1" onClick={this.login}>Zaloguj się</NavItem>,
    <NavItem key="2" onClick={this.login}>Zaloguj się</NavItem>
  ];

Depending on your environment you might not be able to use all of these solutions: support for fragments

like image 53
Kevin Amiranoff Avatar answered Oct 06 '22 00:10

Kevin Amiranoff


In the lastest react version you can wrap them in an empty fragment:

<>
  <NavItem onClick={this.login}>Zaloguj się</NavItem>
  <NavItem onClick={this.login}>Zaloguj się</NavItem>
</>
like image 44
Joel Harkes Avatar answered Oct 05 '22 23:10

Joel Harkes