What could be causing my second React JSX function to fail to return <li> elements, despite properly filtered data?

huangapple go评论62阅读模式
英文:

What could be causing my second React JSX function to fail to return <li> elements, despite properly filtered data?

问题

我正在创建一个在React JSX中的简单搜索栏元素。我尝试渲染包含搜索查询内容的元素列表。基本上,我将所有元素的数组,然后使用.filter()函数查找包含查询的所有内容。然后我使用.map()函数遍历结果,并为每个对象渲染<li>元素。我需要为两个不同的数据集创建两个不同的函数,因为一个数组更深层次。

第一个函数运行良好,并返回了一个<li>元素,正如预期的那样。但是出于某种原因,第二个函数却没有返回任何元素,即使它基本上是相同的代码。奇怪的是数据是存在的,我可以从map函数中记录它,我可以看到它被正确地过滤了。有人能解释一下问题出在哪里吗?我尝试了很多我可能犯的错误,但没有找到任何问题。

英文:

I am creating a simple search bar element in React JSX. I'm trying to render a list of elements that include whatever is in a search query. I basically take an array of all the elements and then I use .filter() function to find everything that includes the query. After that I use .map() function to loop through the results and render <li> elements for each object. I needed to create two different functions for two different datasets as one is an array deeper.

&lt;ul&gt;


{                    
 this.state.searchQuery &amp;&amp;
                                
     this.props.searchDB.projects.filter((project)=&gt; {
         if (this.state.searchQuery === &#39;&#39; || this.state.searchQuery === null) {
            return project;
         } else if (project.projectName.toLowerCase().includes(this.state.searchQuery.toLowerCase())) {
            return project;
         } else {
            return null
         }
     }).map((project, index) =&gt; {
         //THIS WORKS AS EXPECTED
         return(
             &lt;li key={&#39;project_&#39; + index}&gt;
                 {index + &#39;_&#39; + project.projectName}
             &lt;/li&gt;
         )
    })
                                
}

{
     this.state.searchQuery &amp;&amp;
                                
         this.props.searchDB.mentions.forEach((mentionYear) =&gt; {
    
               mentionYear.filter((mention) =&gt; {
    
                   if (this.state.searchQuery === &#39;&#39; || this.state.searchQuery === null) {
                     return mention
                   } else if (mention.mentionTitle.toLowerCase().includes(this.state.searchQuery.toLowerCase())) {
                     return mention
                   } else {
                     return null
                   }
              }).map((mention, mentionIndex) =&gt; {
                   console.log(mention.mentionTitle)
                   //THIS LOGS DATA AS IT SHOULD BUT DOESN&#39;T RENDER ELEMENTS
                                                
                   return(
                            &lt;li key={&#39;project_&#39; + mentionIndex}&gt;
                               {mentionIndex + &#39;_&#39; + mention.mentionTitle}
                            &lt;/li&gt;
                         )
                   })
               }
             ) 
}

&lt;/ul&gt;

The first function works fine and returns a <li> element as it should. For some reason the second one does not and it doesn't return any element at all, even though it is basically the same code. Strange is that the data is there, I can log it from the map function and I can see that it is filtered properly. Can someone explain to me what's wrong? I tried quite a lot of possibile mistakes I could have made but I didn't find anything.

答案1

得分: 1

第二个版本从.map()的结果中不做任何处理。它在forEach()回调内部调用,但其结果被丢弃。与第一个版本相比,第一个版本中.map()的结果是JSX的一部分并进行渲染。

不要使用forEach()来做这个。如果意图是.forEach()迭代应该像第一个版本一样产生一个结果,那么你需要的不是forEach(),而是.map()

例如:

{
  this.state.searchQuery &&
    this.props.searchDB.mentions.map((mentionYear) => {
      return mentionYear.filter((mention) => {
        // etc.
      }).map((mention, mentionIndex) => {
        // etc.
      });
    })
}

基本上,重复你已经知道在JSX中迭代集合以产生渲染结果的相同模式/结构。

英文:

The second version never does anything with the result of .map(). It's invoked inside a forEach() callback, but its result is discarded. Contrast that to the first version where the result of .map() is part of the JSX and rendered.

Don't use forEach() for this. If the intent is that the .forEach() iteration should produce a result just like in the first version, then what you want isn't forEach(). What you want is .map().

For example:

{
  this.state.searchQuery &amp;&amp;
    this.props.searchDB.mentions.map((mentionYear) =&gt; {
      return mentionYear.filter((mention) =&gt; {
        // etc.
      }).map((mention, mentionIndex) =&gt; {
        // etc.
      });
    })
}

Basically, repeat the same pattern/structure you already know works when iterating over a collection in JSX to produce a rendered result.

huangapple
  • 本文由 发表于 2023年6月2日 04:12:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定