简体   繁体   English

renderSortByOptions()有什么作用?

[英]What does renderSortByOptions() do?

I'm studying React and i found a great project i'm replicating and trying to understand. 我正在研究React,发现我正在复制并尝试理解一个很棒的项目。 So far i had no issues with anything except with the renderSortByOptions() . 到目前为止,除了renderSortByOptions()之外,我什么都没有问题。 What exactly is happening there? 那里到底发生了什么? I can see that the Object.key is used on the sortByOptions -object to get the keys (which are Best Match, Highest Rated and Most Reviewed or is it best_match etc.?) but i'm confused why there is a .map . 我可以看到在sortByOptions上使用了Object.key来获取键( Best Match, Highest Rated and Most Reviewed或它是best_match等?),但是我很困惑为什么有一个.map

.map gets a callback-function where a variable (let) gets declared and in it is the sortByOptions -object with sortByOption as an index?! .map获取一个回调函数,其中声明了变量(let),并且其中是sortByOptions其中sortByOption作为索引? I am really confused about this. 我对此很困惑。 kindly explain lines 12 - 18 (basically the renderSortByOptions() ) for me. 请为我解释第12至18行(基本上是renderSortByOptions() )。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> import React from 'react'; import './SearchBar.css'; const sortByOptions = { 'Best Match': 'best_match', 'Highest Rated': 'rating', 'Most Reviewed': 'review_count' }; class SearchBar extends React.Component { renderSortByOptions() { return Object.keys(sortByOptions).map(sortByOption => { let sortByOptionValue = sortByOptions[sortByOption]; return <li key={sortByOptionValue}>{sortByOption}</li>; }); } render() { return ( <div className="SearchBar"> <div className="SearchBar-sort-options"> <ul> {this.renderSortByOptions()} </ul> </div> <div className="SearchBar-fields"> <input placeholder="Search Businesses" /> <input placeholder="Where?" /> </div> <div className="SearchBar-submit"> <a>Let's Go</a> </div> </div> ); } } export default SearchBar; 

Object.keys(obj) gives you an array of keys in obj . Object.keys(obj)为您提供obj的键数组。

So in your case sortByOptions is an object with keys Best Match , Highest Rated and Most Reviewed . 所以你的情况sortByOptions是带有按键的对象Best MatchHighest RatedMost Reviewed

const sortByOptions = {
  'Best Match': 'best_match',
  'Highest Rated': 'rating',
  'Most Reviewed': 'review_count'
};

Object.keys(sortByOptions) will return an array ['Best Match', 'Highest Rated', 'Most Reviewed'] . Object.keys(sortByOptions)将返回一个数组['Best Match', 'Highest Rated', 'Most Reviewed']

So this 所以这

Object.keys(sortByOptions).map(...)

is actually 实际上是

['Best Match', 'Highest Rated', 'Most Reviewed'].map(...)

.map loops over an array returning a new array. .map遍历一个数组,返回一个新数组。

Object.keys(sortByOptions).map(sortByOption => {
  // sortByOption will be each key in the object
  // sortByOptionValue will be the value assigned to that key
  let sortByOptionValue = sortByOptions[sortByOption];
    // you return an <li> element with key and value
    return <li key={sortByOptionValue}>{sortByOption}</li>;
});

The map function is used to iterate an array passing the array items one by one to the callback function, and return values returned in the callback function. map函数用于迭代一个数组,将数组项一一传递给回调函数,并返回在回调函数中返回的值。 As you can see here the rule with React is that each child produced in that iteration needs to have a unique key . 如您所见,React的规则是该迭代中产生的每个子代都需要具有唯一的key

Now the array that the map function is iterating (looping through) is the result (or return value) of Object.keys which returns an array of the keys in the nominated object. 现在, map函数正在迭代(遍历)的数组是Object.keys的结果(或返回值),该结果返回指定对象中键的数组。 In your case the return value of Object.keys(sortByOptions) will be 在您的情况下, Object.keys(sortByOptions)的返回值为

[
  'Best Match',
  'Highest Rated',
  'Most Reviewed'
]

So iterating (looping) that array is what your function renderSortByOptions is doing. 因此,迭代(循环)该数组就是您的函数renderSortByOptions所做的事情。

The variable created inside the callback function is basially the value of the key in the sortByOptions object. 在回调函数内部创建的变量基本上是sortByOptions对象中键的值。 So for the first item in the iteration the value sortByOptions[sortByOption] is best_match , the second is rating and the third would be review_count ; 因此,对于迭代中的第一个项目,值sortByOptions[sortByOption]best_match ,第二个为rating ,第三个为review_count

Basically the result of that functiontion will be 基本上,该功能的结果将是

<li key="best_match">Best Match</li>
<li key="rating">Highest Rated</li>
<li key="review_count">Most Reviewed</li>

of course they need to be children of a <ul> which is taken care of inside the render function 当然,它们必须是<ul>子代,该子代必须在render函数中处理

<ul>
    {this.renderSortByOptions()}
</ul>

Hope that clarifies things a little :) 希望可以澄清一些事情:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM