admin管理员组文章数量:1125397
I'm trying to do something like the following in React JSX (where ObjectRow is a separate component):
<tbody>
for (var i=0; i < numrows; i++) {
<ObjectRow/>
}
</tbody>
I realize and understand why this isn't valid JSX, since JSX maps to function calls. However, coming from template land and being new to JSX, I am unsure how I would achieve the above (adding a component multiple times).
I'm trying to do something like the following in React JSX (where ObjectRow is a separate component):
<tbody>
for (var i=0; i < numrows; i++) {
<ObjectRow/>
}
</tbody>
I realize and understand why this isn't valid JSX, since JSX maps to function calls. However, coming from template land and being new to JSX, I am unsure how I would achieve the above (adding a component multiple times).
Share Improve this question edited Feb 14, 2021 at 15:36 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Apr 5, 2014 at 5:29 B RobsterB Robster 42k23 gold badges91 silver badges124 bronze badges 9 | Show 4 more comments87 Answers
Reset to default 1 2 3 Next 1824Think of it like you're just calling JavaScript functions. You can't use a for
loop where the arguments to a function call would go:
return tbody(
for (let i = 0; i < numrows; i++) {
ObjectRow()
}
)
See how the function tbody
is being passed a for
loop as an argument – leading to a syntax error.
But you can make an array, and then pass that in as an argument:
const rows = [];
for (let i = 0; i < numrows; i++) {
rows.push(ObjectRow());
}
return tbody(rows);
You can basically use the same structure when working with JSX:
const rows = [];
for (let i = 0; i < numrows; i++) {
// note: we are adding a key prop here to allow react to uniquely identify each
// element in this array. see: https://reactjs.org/docs/lists-and-keys.html
rows.push(<ObjectRow key={i} />);
}
return <tbody>{rows}</tbody>;
Incidentally, my JavaScript example is almost exactly what that example of JSX transforms into. Play around with Babel REPL to get a feel for how JSX works.
I am not sure if this will work for your situation, but often map is a good answer.
If this was your code with the for loop:
<tbody>
for (var i=0; i < objects.length; i++) {
<ObjectRow obj={objects[i]} key={i}>
}
</tbody>
You could write it like this with map:
<tbody>
{objects.map(function(object, i){
return <ObjectRow obj={object} key={i} />;
})}
</tbody>
ES6 syntax:
<tbody>
{objects.map((object, i) => <ObjectRow obj={object} key={i} />)}
</tbody>
If you don't already have an array to map()
like @FakeRainBrigand's answer, and want to inline this so the source layout corresponds to the output closer than @SophieAlpert's answer:
With ES2015 (ES6) syntax (spread and arrow functions)
http://plnkr.co/edit/mfqFWODVy8dKQQOkIEGV?p=preview
<tbody>
{[...Array(10)].map((x, i) =>
<ObjectRow key={i} />
)}
</tbody>
Re: transpiling with Babel, its caveats page says that Array.from
is required for spread, but at present (v5.8.23
) that does not seem to be the case when spreading an actual Array
. I have a documentation issue open to clarify that. But use at your own risk or polyfill.
Vanilla ES5
Array.apply
<tbody>
{Array.apply(0, Array(10)).map(function (x, i) {
return <ObjectRow key={i} />;
})}
</tbody>
Inline IIFE
http://plnkr.co/edit/4kQjdTzd4w69g8Suu2hT?p=preview
<tbody>
{(function (rows, i, len) {
while (++i <= len) {
rows.push(<ObjectRow key={i} />)
}
return rows;
})([], 0, 10)}
</tbody>
Combination of techniques from other answers
Keep the source layout corresponding to the output, but make the inlined part more compact:
render: function () {
var rows = [], i = 0, len = 10;
while (++i <= len) rows.push(i);
return (
<tbody>
{rows.map(function (i) {
return <ObjectRow key={i} index={i} />;
})}
</tbody>
);
}
With ES2015 syntax & Array
methods
With Array.prototype.fill
you could do this as an alternative to using spread as illustrated above:
<tbody>
{Array(10).fill(1).map((el, i) =>
<ObjectRow key={i} />
)}
</tbody>
(I think you could actually omit any argument to fill()
, but I'm not 100% on that.) Thanks to @FakeRainBrigand for correcting my mistake in an earlier version of the fill()
solution (see revisions).
key
In all cases the key
attr alleviates a warning with the development build, but isn't accessible in the child. You can pass an extra attr if you want the index available in the child. See Lists and Keys for discussion.
Simply using map Array method with ES6 syntax:
<tbody>
{items.map(item => <ObjectRow key={item.id} name={item.name} />)}
</tbody>
Don't forget the key
property.
Using the Array map function is a very common way to loop through an Array of elements and create components according to them in React. This is a great way to do a pretty efficient and tidy loop in JSX. It's not the only way to do it, but the preferred way.
Also, don't forget to have a unique Key for each iteration as required. The map function creates a unique index from 0, but it's not recommended to use the produced index, but if your value is unique or if there is a unique key, you can use them:
<tbody>
{numrows.map(x=> <ObjectRow key={x.id} />)}
</tbody>
Also, a few lines from MDN if you not familiar with the map function on Array:
map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values, including undefined. It is not called for missing elements of the array (that is, indexes that have never been set, which have been deleted or which have never been assigned a value).
callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.
If a thisArg parameter is provided to the map, it will be used as callback's this value. Otherwise, the value undefined will be used as its this value. This value ultimately observable by the callback is determined according to the usual rules for determining the this seen by a function.
map does not mutate the array on which it is called (although callback, if invoked, may do so).
If you're already using lodash
, the _.times
function is handy.
import React, { Component } from "react";
import Select from "./Select";
import _ from "lodash";
export default class App extends Component {
render() {
return (
<div className="container">
<ol>
{_.times(3, (i) => (
<li key={i}>repeated 3 times</li>
))}
</ol>
</div>
);
}
}
There are multiple ways to go about doing this. JSX eventually gets compiled to JavaScript, so as long as you're writing valid JavaScript, you'll be good.
My answer aims to consolidate all the wonderful ways already presented here:
If you do not have an array of object, simply the number of rows:
Within the return
block, creating an Array
and using Array.prototype.map
:
render() {
return (
<tbody>
{Array(numrows).fill(null).map((value, index) => (
<ObjectRow key={index}>
))}
</tbody>
);
}
Outside the return
block, simply use a normal JavaScript for loop:
render() {
let rows = [];
for (let i = 0; i < numrows; i++) {
rows.push(<ObjectRow key={i}/>);
}
return (
<tbody>{rows}</tbody>
);
}
Immediately invoked function expression:
render() {
return (
<tbody>
{(() => {
let rows = [];
for (let i = 0; i < numrows; i++) {
rows.push(<ObjectRow key={i}/>);
}
return rows;
})()}
</tbody>
);
}
If you have an array of objects
Within the return
block, .map()
each object to a <ObjectRow>
component:
render() {
return (
<tbody>
{objectRows.map((row, index) => (
<ObjectRow key={index} data={row} />
))}
</tbody>
);
}
Outside the return
block, simply use a normal JavaScript for loop:
render() {
let rows = [];
for (let i = 0; i < objectRows.length; i++) {
rows.push(<ObjectRow key={i} data={objectRows[i]} />);
}
return (
<tbody>{rows}</tbody>
);
}
Immediately invoked function expression:
render() {
return (
<tbody>
{(() => {
const rows = [];
for (let i = 0; i < objectRows.length; i++) {
rows.push(<ObjectRow key={i} data={objectRows[i]} />);
}
return rows;
})()}
</tbody>
);
}
You can also extract outside the return block:
render: function() {
var rows = [];
for (var i = 0; i < numrows; i++) {
rows.push(<ObjectRow key={i}/>);
}
return (<tbody>{rows}</tbody>);
}
You might want to checkout React Templates, which does let you use JSX-style templates in React, with a few directives (such as rt-repeat
).
Your example, if you used react-templates
, would be:
<tbody>
<ObjectRow rt-repeat="obj in objects"/>
</tbody>
If you opt to convert this inside return() of the render method, the easiest option would be using the map( ) method. Map your array into JSX syntax using the map() function, as shown below (ES6 syntax is used).
Inside the parent component:
<tbody>
{ objectArray.map(object => <ObjectRow key={object.id} object={object.value} />) }
</tbody>
Please note the key
attribute is added to your child component. If you didn't provide a key attribute, you can see the following warning on your console.
Warning: Each child in an array or iterator should have a unique "key" prop.
Note: One common mistake people do is using index
as the key when iterating. Using index
of the element as a key is an antipattern, and you can read more about it here. In short, if it's not a static list, never use index
as the key.
Now at the ObjectRow component, you can access the object from its properties.
Inside the ObjectRow component
const { object } = this.props
Or
const object = this.props.object
This should fetch you the object you passed from the parent component to the variable object
in the ObjectRow component. Now you can spit out the values in that object according to your purpose.
References:
map() method in JavaScript
ECMAScript 6 or ES6
If numrows is an array, it's very simple:
<tbody>
{numrows.map(item => <ObjectRow />)}
</tbody>
The array data type in React is much better. An array can back a new array, and support filter, reduce, etc.
This can be done in multiple ways.
As suggested above, before
return
store all elements in the arrayLoop inside
return
Method 1:
let container = []; let arr = [1, 2, 3] //can be anything array, object arr.forEach((val, index) => { container.push( <div key={index}> val </div>) /** * 1. All loop generated elements require a key * 2. only one parent element can be placed in Array * e.g. container.push( * <div key={index}> val </div> <div> this will throw error </div> ) **/ }); return ( <div> <div>any things goes here</div> <div>{container}</div> </div> )
Method 2:
return ( <div> <div>any things goes here</div> <div> { (() => { let container = []; let arr = [1, 2, 3] //can be anything array, object arr.forEach((val, index) => { container.push( <div key={index}> val </div>) }); return container; })() } </div> </div> )
There are several answers pointing to using the map
statement. Here is a complete example using an iterator within the FeatureList component to list Feature components based on a JSON data structure called features.
const FeatureList = ({ features, onClickFeature, onClickLikes }) => (
<div className="feature-list">
{features.map(feature =>
<Feature
key={feature.id}
{...feature}
onClickFeature={() => onClickFeature(feature.id)}
onClickLikes={() => onClickLikes(feature.id)}
/>
)}
</div>
);
You can view the complete FeatureList code on GitHub. The features fixture is listed here.
To loop for a number of times and return, you can achieve it with the help of from
and map
:
<tbody>
{
Array.from(Array(i)).map(() => <ObjectRow />)
}
</tbody>
where i = number of times
If you want to assign unique key IDs into the rendered components, you can use React.Children.toArray
as proposed in the React documentation
React.Children.toArray
Returns the children opaque data structure as a flat array with keys assigned to each child. Useful if you want to manipulate collections of children in your render methods, especially if you want to reorder or slice this.props.children before passing it down.
Note:
React.Children.toArray()
changes keys to preserve the semantics of nested arrays when flattening lists of children. That is, toArray prefixes each key in the returned array so that each element’s key is scoped to the input array containing it.
<tbody>
{
React.Children.toArray(
Array.from(Array(i)).map(() => <ObjectRow />)
)
}
</tbody>
Let us say we have an array of items in your state:
[{name: "item1", id: 1}, {name: "item2", id: 2}, {name: "item3", id: 3}]
<tbody>
{this.state.items.map((item) => {
<ObjectRow key={item.id} name={item.name} />
})}
</tbody>
An ECMAScript 2015 / Babel possibility is using a generator function to create an array of JSX:
function* jsxLoop(times, callback)
{
for(var i = 0; i < times; ++i)
yield callback(i);
}
...
<tbody>
{[...jsxLoop(numrows, i =>
<ObjectRow key={i}/>
)]}
</tbody>
ES2015 Array.from with the map function + key
If you have nothing to .map()
you can use Array.from()
with the map
function to repeat elements:
<tbody>
{Array.from({ length: 5 }, (value, key) => <ObjectRow key={key} />)}
</tbody>
...Or you can also prepare an array of objects and map it to a function to have the desired output. I prefer this, because it helps me to maintain the good practice of coding with no logic inside the return of render.
render() {
const mapItem = [];
for(let i =0;i<item.length;i++)
mapItem.push(i);
const singleItem => (item, index) {
// item the single item in the array
// the index of the item in the array
// can implement any logic here
return (
<ObjectRow/>
)
}
return(
<tbody>{mapItem.map(singleItem)}</tbody>
)
}
I use this:
gridItems = this.state.applications.map(app =>
<ApplicationItem key={app.Id} app={app } />
);
PS: never forget the key or you will have a lot of warnings!
Here's a simple solution to it.
var Object_rows = [];
for (var i = 0; i < numrows; i++) {
Object_rows.push(<ObjectRow />);
}
<tbody>{Object_rows}</tbody>;
No mapping and complex code is required. You just need to push the rows to the array and return the values to render it.
Simply use .map()
to loop through your collection and return <ObjectRow>
items with props from each iteration.
Assuming objects
is an array somewhere...
<tbody>
{ objects.map((obj, index) => <ObjectRow obj={ obj } key={ index }/> ) }
</tbody>
You can of course solve with a .map as suggested by the other answer. If you already use Babel, you could think about using jsx-control-statements.
They require a little of setting, but I think it's worth in terms of readability (especially for non-React developer). If you use a linter, there's also eslint-plugin-jsx-control-statements.
Your JSX code will compile into pure JavaScript code, any tags will be replaced by ReactElement
objects. In JavaScript, you cannot call a function multiple times to collect their returned variables.
It is illegal, the only way is to use an array to store the function returned variables.
Or you can use Array.prototype.map
which is available since JavaScript ES5 to handle this situation.
Maybe we can write other compiler to recreate a new JSX syntax to implement a repeat function just like Angular's ng-repeat
.
I tend to favor an approach where programming logic happens outside the return value of render
. This helps keep what is actually rendered easy to grok.
So I'd probably do something like:
import _ from 'lodash';
...
const TableBody = ({ objects }) => {
const objectRows = objects.map(obj => <ObjectRow object={obj} />);
return <tbody>{objectRows}</tbody>;
}
Admittedly this is such a small amount of code that inlining it might work fine.
Here is a sample from the React documentation, JavaScript Expressions as Children:
function Item(props) {
return <li>{props.message}</li>;
}
function TodoList() {
const todos = ['finish doc', 'submit pr', 'nag dan to review'];
return (
<ul>
{todos.map((message) => <Item key={message} message={message} />)}
</ul>
);
}
As for your case, I suggest writing like this:
function render() {
return (
<tbody>
{numrows.map((roe, index) => <ObjectRow key={index} />)}
</tbody>
);
}
Please notice the key is very important, because React use the key to differ data in array.
You may use .map() in a React for loop.
<tbody>
{ newArray.map(() => <ObjectRow />) }
</tbody>
Since you are writing JavaScript syntax inside JSX code, you need to wrap your JavaScript code in curly braces.
row = () => {
var rows = [];
for (let i = 0; i<numrows; i++) {
rows.push(<ObjectRow/>);
}
return rows;
}
<tbody>
{this.row()}
</tbody>
With time, the language is becoming more mature, and we often stumble upon common problems like this. The problem is to loop a Component 'n' times.
{[...new Array(n)].map((item, index) => <MyComponent key={index} />)}
where, n -is the number of times you want to loop. item
will be undefined and index
will be as usual. Also, ESLint discourages using an array index as key.
But you have the advantage of not requiring to initialize the array before and most importantly avoiding the for
loop...
To avoid the inconvenience of item as undefined you can use an _
, so that it will be ignored when linting and won't throw any linting error, such as
{[...new Array(n)].map((_, index) => <MyComponent key={index} />)}
If you really want a for loop equivalent (you have a single number, not an array), just use range
from Lodash.
Don't reinvent the wheel and don't obfuscate your code. Just use the standard utility library.
import range from 'lodash/range'
range(4);
// => [0, 1, 2, 3]
range(1, 5);
// => [1, 2, 3, 4]
You can also use a self-invoking function:
return <tbody>
{(() => {
let row = []
for (var i = 0; i < numrows; i++) {
row.push(<ObjectRow key={i} />)
}
return row
})()}
</tbody>
本文标签: javascriptLoop inside React JSXStack Overflow
版权声明:本文标题:javascript - Loop inside React JSX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736660321a1946382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
let todos = this.props.todos.map((todo) => {return <h1>{todo.title}</h1>})
– OverCoder Commented Jan 15, 2017 at 19:58let todos = this.props.todos.map(t => <h1>{t.title}</h1>)
:) – OverCoder Commented Mar 17, 2020 at 10:16