ReactJS - 列表



列表和 For 迴圈

React 中最常見的模式是將專案的集合轉換為 React 元素。JavaScript 有很多選項可以操作集合。讓我們看看如何在本章中使用for迴圈來使用集合。

for迴圈

一個簡單易用的解決方案是使用經過時間考驗的 for 迴圈來遍歷集合,並使用 JSX 表示式建立最終的 React 元素。讓我們建立一個 React 應用並嘗試應用 for 迴圈。

使用 create-react-app 建立一個新應用並啟動應用。

create-react-app myapp
cd myapp
npm start

接下來,在 components 資料夾下建立一個名為 ExpenseListUsingForLoop 的元件 (src/components/ExpenseListUsingForLoop.js)

import React from 'react'
class ExpenseListUsingForLoop extends React.Component {
   render() {
      return <table>
         <thead>
            <tr>
               <th>Item</th>
               <th>Amount</th>
            </tr>
         </thead>
         <tbody>
         
         </tbody>
         <tfoot>
            <tr>
               <th>Sum</th>
               <th></th>
            </tr>
         </tfoot>
      </table>
   }
}
export default ExpenseListUsingForLoop

在這裡,我們建立了一個帶有表頭和表尾的基本表格結構。

接下來,建立一個函式來查詢總支出金額。我們稍後將在render()方法中使用它。

getTotalExpenses() {
   var items = this.props['expenses'];
   var total = 0;
   for(let i = 0; i < items.length; i++) {
      total += parseInt(items[i]);
   }
   return total;
}

在這裡,getTotalExpenses 遍歷 expense props 並彙總總支出。然後,在 render 方法中新增支出專案和總金額。

render() {
   var items = this.props['expenses'];
   var expenses = []
   for(let i = 0; i < items.length; i++) {
      expenses.push(<tr><td>item {i + 1}</td><td>{items[i]}</td></tr>)
   }
   var total = this.getTotalExpenses();
   return <table>
      <thead>
         <tr>
            <th>Item</th>
            <th>Amount</th>
         </tr>
      </thead>
      <tbody>
         {expenses}
      </tbody>
      <tfoot>
         <tr>
            <th>Sum</th>
            <th>{total}</th>
         </tr>
      </tfoot>
   </table>
}

在這裡,

  • 使用 for 迴圈遍歷 expense 陣列中的每個專案,使用 JSX 生成表格行 (tr),最後將其推送到 expenses 陣列中。

  • 我們在 JSX 表示式中使用了expenses陣列來包含生成的行。

  • getTotalExpenses方法用於查詢總支出金額並將其新增到 render 方法中。

ExpenseListUsingForLoop元件的完整原始碼如下:

import React from 'react'
class ExpenseListUsingForLoop extends React.Component {
   getTotalExpenses() {
      var items = this.props['expenses'];
      var total = 0;
      
      for(let i = 0; i < items.length; i++) {
         total += parseInt(items[i]);
      }
      return total;
   }
   render() {
      var items = this.props['expenses'];
      var expenses = []
      for(let i = 0; i < items.length; i++) {
         expenses.push(<tr><td>item {i + 1}</td><td>{items[i]}</td></tr>)
      }
      var total = this.getTotalExpenses();
      
      return <table>
         <thead>
            <tr>
               <th>Item</th>
               <th>Amount</th>
            </tr>
         </thead>
         <tbody>
            {expenses}
         </tbody>
         <tfoot>
            <tr>
               <th>Sum</th>
               <th>{total}</th>
            </tr>
         </tfoot>
      </table>
   }
}
export default ExpenseListUsingForLoop

接下來,使用ExpenseListUsingForLoop元件更新 App 元件 (App.js)。

import ExpenseListUsingForLoop from './components/ExpenseListUsingForLoop';
import './App.css';
function App() {
   var expenses = [100, 200, 300]
   return (
      <div>
         <ExpenseListUsingForLoop expenses={expenses} />
      </div>
   );
}
export default App;

接下來,在 App.css 中新增一些基本樣式。

/* Center tables for demo */
table {
   margin: 0 auto;
}
div {
   padding: 5px;
}
/* Default Table Style */
table {
   color: #333;
   background: white;
   border: 1px solid grey;
   font-size: 12pt;
   border-collapse: collapse;
}
table thead th,
table tfoot th {
   color: #777;
   background: rgba(0,0,0,.1);
   text-align: left;
}
table caption {
   padding:.5em;
}
table th,
table td {
   padding: .5em;
   border: 1px solid lightgrey;
}

最後,在瀏覽器中檢查應用。它將顯示如下所示的支出:

List and For

ECMASCript 6 for迴圈

讓我們更改應用並使用 ECMAScript 6 中引入的for .. of迴圈。

for(const [idx, item] of items.entries()) {
   expenses.push(<tr><td>item {idx + 1}</td><td>{item}</td></tr>)
}

在這裡,

  • idx 指的是陣列的索引。

  • item 指的是陣列每個位置的支出專案。

  • entries 方法解析陣列並將其作為鍵值對的陣列返回。key 指的是陣列的索引,value 指的是對應鍵的陣列值。

如果我們不需要索引,那麼我們可以跳過entries()方法,如下所示:

for(const item of items) {
   expenses.push(<tr><td></td><td>{item}</td></tr>)
}
廣告