簡體   English   中英

使用 React 將一個項目傳播到數組的開頭

[英]Spread an item to the beginning of the array with React

這個想法是將“Home”項添加到 renderBreadcrumbsList 數組的開頭。 問題是它以遞歸方式呈現,因此我並不真正關心這種機制。 想法?

當前代碼邏輯:

  1. 遍歷對象的面包屑數組
  2. 使用renderBreadcrumb()方法渲染每個面包屑

要求:

  1. 再將一個renderBreadcrumb()項傳播到最終(映射)數組的開頭
// The following custom item should be spread to the array
renderBreadcrumb({ url, name }, i) {
  const { breadcrumbs } = this.props;
  const isDisabled = !url || breadcrumbs.length - 1 === i;

  return (
    <Breadcrumb
      name={ name }
      url={ url }
      index={ i }
      key={ i }
      isDisabled={ isDisabled }
    />
  );
}

// This should be added here
renderBreadcrumbList(breadcrumbs) {
  return breadcrumbs.map((_, i) => this.renderBreadcrumb(
  breadcrumbs[breadcrumbs.length - 1 - i], i
));
}

這個想法是將“Home”項添加到 renderBreadcrumbsList 數組的開頭。 問題是它以遞歸方式呈現,因此我並不真正關心這種機制。 想法?

當前代碼邏輯:

  1. 遍歷對象的面包屑數組
  2. 使用renderBreadcrumb()方法渲染每個面包屑

要求:

  1. 再將一個renderBreadcrumb()項傳播到最終(映射)數組的開頭
// The following custom item should be spread to the array
renderBreadcrumb({ url, name }, i) {
  const { breadcrumbs } = this.props;
  const isDisabled = !url || breadcrumbs.length - 1 === i;

  return (
    <Breadcrumb
      name={ name }
      url={ url }
      index={ i }
      key={ i }
      isDisabled={ isDisabled }
    />
  );
}

// This should be added here
renderBreadcrumbList(breadcrumbs) {
  return breadcrumbs.map((_, i) => this.renderBreadcrumb(
  breadcrumbs[breadcrumbs.length - 1 - i], i
));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM