reactjs - 调整 HTML 在地图循环中的呈现位置
问题描述
我有一组要在页面上呈现的 HTML 元素,但取决于元素,我想调整它们的包装方式。
const sections = [
{
id: 'top',
},
{
id: 'left',
},
{
id: 'right',
},
{
id: 'bottom',
}
]
const Element = (props) => {
return <div id={props.id}>hello</div>
}
const ArticleRightRail = (props) =>
<div>
<header>
</header>
<article>
{sections.map((section, i) => <Element key={i} {...section} >hello!</Element> )}
</article>
</div>
在上面的示例中,我想要任何id
不在标签内top
或将在标签内bottom
呈现的内容,以及在标签内呈现或将要呈现的<article>
任何内容。使用 React 处理这种情况的最佳方法是什么?top
bottom
<header>
解决方案
@JamesIves,反应代码是:
const listForHeader = sections.filter((section, i) => {
return section === "top" || section === "bottom";
});
const listForArticle = sections.filter((section, i) => {
return section != "top" || section != "bottom";
})
const ArticleRightRail = (props) =>
<div>
<header>
{listForHeader.map((section, i) => <Element key={i} {...section} >hello!</Element> )}
</header>
<article>
{listForArticle.map((section, i) => <Element key={i} {...section} >hello!</Element> )}
</article>
</div>
推荐阅读
- python - 从字符串列表中,创建新列表,其中每个项目指示原始列表中的相应项目是否在两个特定条目之间
- javascript - 如何在不同的上下文中使用 es6 构造函数指令
- excel - 从列创建唯一列表 使用功能区栏在下拉列表中显示结果 在工作表上显示过滤器
- firebase - 每个用户每 100 秒的读取请求 - 不影响 Firebase 功能
- java - 在 java ( Android ) 中实例化接口
- java - 如何使用 try catch 来避免抛出异常?
- python - 在线程 Python 中构造类
- python-3.x - 执行中间人攻击时出现 Scapy 错误
- jquery - 无法在进行 ajax 调用时触发成功或失败事件
- python-3.x - 如何将包含重复字母的单词还原为其原始英文形式?