在 JSX 中有条件输出 ul 标签?

发布于 2025-01-18 06:44:29 字数 2994 浏览 1 评论 0原文

设置:无头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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2025-01-25 06:44:29

您只需要将条件放在上面,并确保检查长度的状况即可。

{citationPath && citationPath.length > 0 && (
  <ul>
    {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>
)}

由于这同时检查了长度true citationpath的值我在生产中使用此代码。

You just need to put the condition above and make sure to check the condition of length.

{citationPath && citationPath.length > 0 && (
  <ul>
    {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>
)}

Since this checks both the length and the truethy value of citationPath, this is the solution you're looking for and as an industry professional, I use this code in production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文