[英]Render array items within groups as separate JSX elements
I have an array say arr chapters = ["C1", "C2", "C3,...."C10", "C11"] with 11 chapter titles. I am trying to render these inside <ul>
as groups of max 5 items in each <ul>
. So, for example:我有一个数组说 arr Chapters = ["C1", "C2", "C3,...."C10", "C11"] 有 11 个章节标题。我试图将这些在
<ul>
呈现为一组每个<ul>
最多 5 个项目。因此,例如:
<ul>
<li>C1</li>
<li>C2</li>
<li>C3</li>
<li>C4</li>
<li>C5</li>
</ul>
<ul>
<li>C6</li>
<li>C7</li>
<li>C8</li>
<li>C9</li>
<li>C10</li>
</ul>
<ul>
<li>C11</li>
</ul>
I have passed on the array object to my component but I am having a hard time to figure out how to implement the conditional rendering part.我已将数组对象传递给我的组件,但我很难弄清楚如何实现条件渲染部分。 This is inside (a bigger) render function:
这是(一个更大的)渲染函数内部:
<ul>
{data.chapters && data.chapters.map((chapter,index) => (
<li>{chapter}</li>
//??How to conditionally add </ul><ul> here to start grouping them into 5 items
))}
</ul>
I am thinking of using the index value to help me out identify the item number and conditionally render a closing </ul><ul>
tag to close and open a new <ul>
tag.我正在考虑使用索引值来帮助我确定项目编号并有条件地呈现关闭
</ul><ul>
标记以关闭和打开新的<ul>
标记。
I am also thinking, maybe I should be putting the logic outside of the render() and pre-sort the items into arrays of 5 and then render them here.我也在想,也许我应该将逻辑放在 render() 之外,并将项目预先排序为 5 个数组,然后在此处渲染它们。
Return tags based on index.根据索引返回标签。
For first element in the group return <ul>
and for last return </ul>
对于组中的第一个元素返回
<ul>
和最后一个返回</ul>
<ul>
{data.chapters && data.chapters.map((chapter,index) => {
if(index%5===1) return <ul><li>{chapter}</li>
else if(index%5===0 || index+1===data.chapters.length) return <li>{chapter}</li></ul>
else return <li>{chapter}</li>
})}
</ul>
try this:尝试这个:
{data.chapters && data.chapters.map((chapter,index) => {
return (index+1) % 5 === 0 && <ul><li>{chapter}</li></ul>
})}
Try this:尝试这个:
const courses = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11"];
const getItems = function() {
const lists = [];
let listItems = [];
for(let loopIndex = 0; loopIndex < courses.length; loopIndex++) {
const nextIndex = (loopIndex + 1);
listItems.push(<li>{ courses[loopIndex] }</li>);
if(((nextIndex % 5) === 0) || nextIndex === courses.length) {
lists.push(<ul>{ listItems }</ul>);
listItems = [];
}
}
return lists;
};
And then you call getItems
inside render
:然后在
render
调用getItems
:
render() {
return (<>{ getItems() }</>);
}
import React from 'react';
import './card.css';
import _ from 'lodash';
class Chapters extends React.Component {
state = {
chunk: _.chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 5)
}
render() {
return (
<div>
{this.state.chunk.map((part, index) => {
return <ul>
Group - {index}
{part.map((ch, i) => {
return <li>{ch}</li>
})}
</ul>
})}
</div>
)
}
}
export default Chapters;
The problem was to create dynamic iteration of <ul> </ul>
.As the above person describing in the question.问题是创建
<ul> </ul>
动态迭代。正如上述人在问题中所描述的。
So I have used lodash chunk function which will return two-dimensional array const chunks = [[1,2,3,4,5], [6,7,8,9,10], [11]];
所以我使用了 lodash chunk函数,它将返回二维数组
const chunks = [[1,2,3,4,5], [6,7,8,9,10], [11]];
chunks[0] = <ul> </ul>
chunks[0] = <ul> </ul>
chunks[0] = <ul> </ul>
chunks[0] =
<ul> </ul>
chunks[0] = <ul> </ul>
chunks[0] = <ul> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.