- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 特性
- ReactJS - 優點與缺點
- ReactJS - 架構
- ReactJS - 建立 React 應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在支出管理器應用中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理 API
- ReactJS - 無狀態元件
- ReactJS - 使用 React Hooks 進行狀態管理
- ReactJS - 使用 React Hooks 的元件生命週期
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http 客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - Map
- ReactJS - 表格
- ReactJS - 使用 Flux 管理狀態
- ReactJS - 測試
- ReactJS - CLI 命令
- ReactJS - 構建和部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks 簡介
- ReactJS - 使用 useState
- ReactJS - 使用 useEffect
- ReactJS - 使用 useContext
- ReactJS - 使用 useRef
- ReactJS - 使用 useReducer
- ReactJS - 使用 useCallback
- ReactJS - 使用 useMemo
- ReactJS - 自定義 Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - Fragments
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 最佳化效能
- ReactJS - Profiler API
- ReactJS - Portals
- ReactJS - 無 ES6 ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - 渲染 Props
- ReactJS - 靜態型別檢查
- ReactJS - Strict Mode
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - 列表支出
開啟 ExpenseEntryItemList.js 並從 redux 庫匯入 connect。
import { connect } from 'react-redux';
接下來,匯入 addExpenseList 和 deleteExpense actions。
import { getExpenseList, deleteExpense } from '../actions/expenseActions';
接下來,使用 props 新增建構函式。
constructor(props) {
super(props);
}
接下來,在 componentDidMount() 生命週期中呼叫 getExpenseList。
componentDidMount() {
this.props.getExpenseList();
}
接下來,編寫一個方法來處理刪除支出選項。
handleDelete = (id,e) => {
e.preventDefault();
this.props.deleteExpense(id);
}
現在,讓我們編寫一個函式 getTotal 來計算總支出。
getTotal() {
let total = 0;
if (this.props.expenses != null) {
for (var i = 0; i < this.props.expenses.length; i++) {
total += this.props.expenses[i].amount
}
}
return total;
}
接下來,更新 render 方法並列出支出專案。
render() {
let lists = [];
if (this.props.expenses != null) {
lists = this.props.expenses.map((item) =>
<tr key={item.id}>
<td>{item.name}</td>
<td>{item.amount}</td>
<td>{new Date(item.spendDate).toDateString()}</td>
<td>{item.category}</td>
<td><a href="#"
onClick={(e) => this.handleDelete(item.id, e)}>Remove</a></td>
</tr>
);
}
return (
<div>
<table>
<thead>
<tr>
<th>Item</th>
<th>Amount</th>
<th>Date</th>
<th>Category</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
{lists}
<tr>
<td colSpan="1" style={{ textAlign: "right" }}>Total Amount</td>
<td colSpan="4" style={{ textAlign: "left" }}>
{this.getTotal()}
</td>
</tr>
</tbody>
</table>
</div>
);
}
接下來,編寫 mapStateToProps 和 mapDispatchToProps 方法。
const mapStateToProps = state => {
return {
expenses: state.data
};
};
const mapDispatchToProps = {
getExpenseList,
deleteExpense
};
在這裡,我們將 redux store 中的支出專案對映到 expenses 屬性,並將排程器 getExpenseList 和 deleteExpense 附加到元件屬性。
最後,使用 connect api 將元件連線到 Redux store。
export default connect( mapStateToProps, mapDispatchToProps )(ExpenseEntryItemList);
完整的應用程式原始碼如下所示:
import React from "react";
import { connect } from 'react-redux';
import { getExpenseList, deleteExpense } from '../actions/expenseActions';
class ExpenseEntryItemList extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.props.getExpenseList();
}
handleDelete = (id, e) => {
e.preventDefault();
this.props.deleteExpense(id);
}
getTotal() {
let total = 0;
if (this.props.expenses != null) {
for (var i = 0; i < this.props.expenses.length; i++) {
total += this.props.expenses[i].amount
}
}
return total;
}
render() {
let lists = [];
if (this.props.expenses != null) {
lists = this.props.expenses.map((item) =>
<tr key={item.id}>
<td>{item.name}</td>
<td>{item.amount}</td>
<td>{new Date(item.spendDate).toDateString()}</td>
<td>{item.category}</td>
<td><a href="#"
onClick={(e) => this.handleDelete(item.id, e)}>Remove</a>
</td>
</tr>
);
}
return (
<div>
<table>
<thead>
<tr>
<th>Item</th>
<th>Amount</th>
<th>Date</th>
<th>Category</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
{lists}
<tr>
<td colSpan="1" style={{ textAlign: "right" }}>Total Amount</td>
<td colSpan="4" style={{ textAlign: "left" }}>
{this.getTotal()}
</td>
</tr>
</tbody>
</table>
</div>
);
}
}
const mapStateToProps = state => {
return {
expenses: state.data
};
};
const mapDispatchToProps = {
getExpenseList,
deleteExpense
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(ExpenseEntryItemList);
接下來,使用 npm 命令啟動應用程式。
npm start
接下來,開啟瀏覽器並在位址列中輸入 https://:3000 並按回車鍵。
reactjs_example.htm
廣告