Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return multiple lines JSX in another return statement in React?

A single line works fine:

render: function () {   return (     {[1,2,3].map(function (n) {       return <p>{n}</p>     }}   ); } 

But not for multiple lines:

render: function () {   return (     {[1,2,3].map(function (n) {       return (         <h3>Item {n}</h3>         <p>Description {n}</p>       )     }}   ); } 
like image 619
Shawn Avatar asked May 24 '14 03:05

Shawn


People also ask

How do I return multiple lines in React?

We can return multiple lines of JSX code in when we map them from an array by returning an array in the map callback.

How do I comment multiple lines in JSX?

Multi-Line Comment If you want to comment something in JSX you need to use JavaScript comments inside of Curly braces like {/*comment here*/}. It is a regular /* Block Comments */ , but need to be wrapped in curly braces.

How do I return a JSX map?

The answer is, you use Array. map() in your component and return JSX elements inside the Array. map() callback function to render the UI.

Can I use forEach in JSX?

To use . forEach() or other JavaScript methods within JSX, you must place the expressions between curly braces. This is necessary to let React know to treat everything between curly braces as regular JavaScript.

How to return an array of JSX elements in ReactJS?

One solution is to to return an array of JSX elements: Another solution is to use Fragment, a relatively new React feature that solves the problem for us: it works like the div element we added before, but it’s not going to appear in the resulting HTML rendered to the browser. Win-win.

How to return multiple elements from a component in react?

From React v16.0.0 onwards, it is possible to return multiple elements by wrapping them within an Array: Also from React v16.2.0, a new feature called React Fragments is introduced which you can use to wrap multiple elements: A common pattern in React is for a component to return multiple elements.

How to return more than one node from a JSX component?

Currently, in a component's render, you can only return one node; if you have, say, a list of divs to return, you must wrap your components within a div, span or any other component. Don't forget that JSX compiles into regular JS; returning two functions doesn't really make syntactic sense. Likewise, don't put more than one child in a ternary.

Can a table have multiple body types in react?

I wrapped them in a <tbody> – a table is allowed to have multiple bodies. As of React 16.0, returning an array is apparently allowed again, as long as each element has a key: New render return types: fragments and strings


1 Answers

Try to think of the tags as function calls (see the documentation). Then the first one becomes:

{[1,2,3].map(function (n) {   return React.DOM.p(...); })} 

And the second one:

{[1,2,3].map(function (n) {   return (     React.DOM.h3(...)     React.DOM.p(...)   ) })} 

It should now be clear that the second snippet doesn't really make sense (you can't return more than one value in JavaScript). You have to either wrap it in another element (most likely what you'd want, that way you can also provide a valid key property), or you can use something like this:

{[1,2,3].map(function (n) {   return ([     React.DOM.h3(...),     React.DOM.p(...)   ]); })} 

With JSX syntactic sugar:

{[1,2,3].map(function (n) {   return ([     <h3></h3>, // note the comma     <p></p>   ]); })} 

You don't need to flatten the resulting array. React will do that for you. See the following fiddle http://jsfiddle.net/mEB2V/1/. Again: Wrapping the two elements into a div/section will most likely be better long term.

like image 82
Jan Olaf Krems Avatar answered Sep 28 '22 23:09

Jan Olaf Krems