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> ) }} ); }
We can return multiple lines of JSX code in when we map them from an array by returning an array in the map callback.
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.
The answer is, you use Array. map() in your component and return JSX elements inside the Array. map() callback function to render the UI.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With