Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple elements inside React.render()

I am new in react and I encountered with this problem:

render: function(){
    return (
        <h3>Account</h3>
        <a href="#" onClick={some_event}>Login</a>
        <a href="#" onClick={some_event}>Logout</a>
)}

When I am rendering like this it gives me error saying like multiple components must wrapt with end

Should I make one componenet for each html tag or each line or I can render in that way..

Any suggestion ?

like image 274
varad Avatar asked Jan 20 '16 07:01

varad


People also ask

How do I return multiple components 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.

How do I display multiple elements in React?

In Vue and React, we can only render one element. Even if we have multiple elements to render, there can only be a single root element. This means if we want to render two or more elements, we have to wrap them in another element or component. Commonly, the element used for this is a <div> tag.

Why can't the component directly return multiple elements?

When you return multiple elements from React elements from the render method, the assumtion that the tree will have one root node for the Component will not longer hold, hence making it difficult to process reconcilation algorithm. Thus react gives you a limitation to provide a root node.

How do you return an element in React?

In React v16 it's possible for render() to return an array of elements. Another option is to use a Fragment. Fragments let you group a list of children without adding extra nodes to the DOM.


Video Answer


3 Answers

In React < v16.0 the render method can only render a single root node. (update: this is changed in v16, see below). In your case, you're returning 3 nodes. To get around this you can wrap your 3 nodes in a single root node:

render: function(){
  return (
    <div>
      <h3>Account</h3>
      <a href="#" onClick={some_event}>Login</a>
      <a href="#" onClick={some_event}>Logout</a>
    </div>
)}

In React v16 it's possible for render() to return an array of elements.

Like with other arrays, you’ll need to add a key to each element to avoid the key warning:

render() {
  return [
    <ChildA key="key1" />,
    <ChildB key="key2" />,
    <ChildC key="key3" />,
  ];
}

Another option is to use a Fragment. Fragments let you group a list of children without adding extra nodes to the DOM.

render() {
  return (
    <React.Fragment>
      <ChildA />
      <ChildB />
      <ChildC />
    </React.Fragment>
  );
}

There is also a short syntax (<>) for declaring fragments:

render() {
  return (
    <>
      <ChildA />
      <ChildB />
      <ChildC />
    </>
  );
}
like image 139
Brett DeWoody Avatar answered Oct 13 '22 07:10

Brett DeWoody


Return an array of elements, separated by comma.

render: function(){
  return ([
    <h3>Account</h3>,
    <a href="#" onClick={some_event}>Login</a>,
    <a href="#" onClick={some_event}>Logout</a>
  ])
}
like image 20
lulalala Avatar answered Oct 13 '22 08:10

lulalala


You can use the following syntax for fragments in React 16.2+:

render() {
  return (
    <>
      <ChildA />
      <ChildB />
      <ChildC />
    </>
  );
}
like image 10
Sisyphus Avatar answered Oct 13 '22 06:10

Sisyphus