重构筛选器和映射成搜索栏的部分

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

Refactoring Filter and Map into sections for search bar

问题

Sure, here's the translated code portion:

return (
  <div>
    <input placeholder="搜索图书标题" onChange={handleChange} />
    {
      bookList.filter(book => {
        let isMatched = (book.title.toLowerCase().includes(query.toLowerCase()));

        if (query === '') {
          return ;
        } else if (isMatched) {
          return book;
        }
      }).map((book, index) => (
        <div>			
          这是这本书
        </div>
      ))
    }
  </div>
);
};

Please note that the translation doesn't change the code's functionality, only the comments and placeholder text have been translated.

英文:

I have a search bar component. I would like to know how I can refactor the below code such that I can include html markup to display if there is no match. Right now the markup displays if there is a match or nothing displays if its blank.

I was getting errors when separating out the filter and map function.

return (
&lt;div&gt;
&lt;input placeholder=&quot;Search Book Title&quot; onChange={handleChange} /&gt;
    	{
    	bookList.filter(book =&gt; {
    		let isMatched = (book.title.toLowerCase().includes(query.toLowerCase()));
    
    		if (query === &#39;&#39;) {
    			return ;
    		} else if (isMatched) {
    			return book;
    		}
    	}).map((book, index) =&gt; (
    		&lt;div&gt;			
    		This is the book   
    		&lt;/div&gt;
    	))
    	}
    &lt;/div&gt;
)
};

I was getting errors when separating out the filter and map function.

答案1

得分: 0

请注意,您的代码部分无需翻译,以下是翻译好的部分:

做过滤器并将结果赋给一个变量。检查.length是否为零,如果是,则返回您的“未找到书籍”标记。否则返回找到的书籍列表。例如

function getFilteredBooks() {
  let filteredBooks = bookList.filter(book => 
    book.title.toLowerCase().includes(query.toLowerCase())
  );

  if (filteredBooks.length === 0) {
    return (<div>未找到书籍</div>);
  } else {
    return books.map(book => <div>{{book.title}}</div>);
  }
}

// ... 其他代码

return (<div> <input placeholder="搜索书名" onChange={handleChange} />{getFilteredBooks()}</div>)
英文:

Do the filter first and assign the result to a variable. Check if the .length is zero, if it is return your no books found markup. Otherwise return your books that were found list. For example

function getFilteredBooks() {
  let filteredBooks = bookList.filter(book =&gt; 
    book.title.toLowerCase().includes(query.toLowerCase())
  );

  if (filteredBooks.length === 0) {
    return (&lt;div&gt;No books&lt;/div&gt;);
  } else {
    return books.map(book =&gt; &lt;div&gt;{{book.title}}&lt;/div&gt;);
  }
}

// ... other code

return (&lt;div&gt; &lt;input placeholder=&quot;Search Book Title&quot; onChange={handleChange} /&gt;{{getFilteredBooks()}}&lt;/div&gt;)

答案2

得分: 0

I would recommend assigning the result of the filtered books to a variable and then use it in your rendered result -

const filteredBooks = bookList.filter(book => {
  return query !== ""
    && book.title.toLowerCase().includes(query.toLowerCase());
})
      
return (
  <div>
    <input placeholder="Search Book Title" onChange={handleChange} />
    {filteredBooks.length === 0
      ? <p>No matching books</p>
      : filteredBooks.map((book, index) => (
          <div key={index}>           
            This is the book
          </div>
        ))
    }
  </div>
)

If the filter is an expensive computation, you should useMemo -

const filteredBooks = useMemo(() => (
  bookList.filter(book => (
    query !== ""
      && book.title.toLowerCase().includes(query.toLowerCase())
      && someExpensiveCondition(query) // <-
  ))
), [bookList, query])
英文:

I would recommend assigning the result of the filtered books to a variable and then use it in your rendered result -

const filteredBooks = bookList.filter(book =&gt; {
  return query != &quot;&quot;
    &amp;&amp; book.title.toLowerCase.includes(query.toLowerCase())
})
      
return (
  &lt;div&gt;
    &lt;input placeholder=&quot;Search Book Title&quot; onChange={handleChange} /&gt;
    { filteredBooks.length == 0
    ? &lt;p&gt;No matching books&lt;/p&gt;
    : filteredBook.map((book, index) =&gt; (
        &lt;div key={index}&gt;           
          This is the book   
        &lt;/div&gt;
      ))
    }
  &lt;/div&gt;
)

If the filter is an expensive computation, you should useMemo -

const filteredBooks = useMemo(() =&gt; (
  bookList.filter(book =&gt; (
    query != &quot;&quot;
      &amp;&amp; book.title.toLowerCase.includes(query.toLowerCase())
      &amp;&amp; someExpensiveCondition(query) // &lt;-
  ))
), [bookList, query])

huangapple
  • 本文由 发表于 2023年4月20日 02:41:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057859.html
匿名

发表评论

匿名网友

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

确定