在 JSX 中有条件输出 ul 标签?
设置:无头WordPress CM,gatsbyjs,GraphQl
我认为这是一个JavaScript ES6问题,而不是gatsbyjs问题。
我有一个查询,可以输出作者,标题和URL。 数据是从ACF中继器字段查询的,因此可能有一个,没有一个或多个项目。
这是代码:
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
这有效,没问题。
我现在想在代码之前放置一个&lt; ul&gt;
标记,然后仅当某些citation> citation> citation> citation> >项目 退还。
选项#1
我可以做到这一点:
{citationPath &&
citationPath.map(item => {
return (
<ul>
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
</ul>
)
})}
但是然后我得到了每个列表项目周围的&lt; ul&gt;/ul&gt;
。
选项#2
我可以做到这一点:
<ul>
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
</ul>
但是,当没有citationpath
返回项目时,我会得到&lt; ul&gt;/ul&gt;
。
当我尝试此操作时,选项#3
:
{citationPath && parse("<ul>")}
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
{citationPath && parse("</ul>")}
在那些不返回citationpath
数据的帖子上没有打印HTML。但是,在那些我会得到以下HTML输出的情况下:
<ul></ul>
<li>
<a href=[my url] target="_blank" rel="noopener noreferrer">
<li>
[more list items ...]
因此,看来{CitationPath&amp;&amp; parse(“&lt; ul&gt;”)}
and {citationpath&amp;&amp;&amp; parse(“&lt;/ul&gt;“)}
在以下之前执行:
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
注意:我不简单地选择选项#2的原因是我不希望屏幕读取器撞到空&lt中; ul&gt;&lt;/ul&gt;
标签。
有人对如何解决这个问题有任何想法吗?
Setup: Headless Wordpress CMS, GatsbyJs, GraphQL
I think this is a JavaScript ES6 problem rather than a GatsbyJS problem.
I've got a query that outputs an author, title and url.
The data is queried from an ACF repeater field, so there could be one, none or many items.
Here's the code:
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
THIS WORKS, no problem.
I now want to put a <ul>
tag before the code, and a </ul>
tag after the code ONLY if some citationPath
items
are returned.
OPTION #1
I could do this:
{citationPath &&
citationPath.map(item => {
return (
<ul>
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
</ul>
)
})}
but then I get a <ul></ul>
around each list item.
OPTION #2
I could do this:
<ul>
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
</ul>
but then I get <ul></ul>
when NO citationPath
items are returned.
OPTION #3
When I try this:
{citationPath && parse("<ul>")}
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
{citationPath && parse("</ul>")}
No HTML is printed out on those posts which do not return citationPath
data. However, on those that do I get the following HTML output:
<ul></ul>
<li>
<a href=[my url] target="_blank" rel="noopener noreferrer">
<li>
[more list items ...]
So it seems that {citationPath && parse("<ul>")}
and {citationPath && parse("</ul>")}
are being executed before:
{citationPath &&
citationPath.map(item => {
return (
<li key={item.url}>
<a href={item.url} target="_blank" rel="noopener noreferrer">
{item.title}
</a>
<p>Author: {item.author}</p>
</li>
)
})}
Note: the reason I don't simply go for OPTION #2 is that I don't want screen readers bumping into empty <ul></ul>
tags.
Does any one have any ideas on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要将条件放在上面,并确保检查长度的状况即可。
由于这同时检查了
长度
和true
citationpath的值我在生产中使用此代码。
You just need to put the condition above and make sure to check the condition of length.
Since this checks both the
length
and thetrue
thy value ofcitationPath
, this is the solution you're looking for and as an industry professional, I use this code in production.