- 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 - 在 Expense Manager APP 中引入事件
- 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 - Render 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 - 使用元件
React 元件代表網頁中一小塊使用者介面。React 元件的主要工作是渲染其使用者介面並在其內部狀態發生更改時更新它。除了渲染 UI 外,它還管理屬於其使用者介面的事件。總而言之,React 元件提供以下功能。
在 ReactJS 中使用元件
在本章中,讓我們使用新建立的元件並增強我們的 ExpenseEntryItem 元件。
步驟 1 - 在您喜歡的編輯器中開啟我們的 expense-manager 應用程式,並開啟 ExpenseEntryItem.js 檔案。
然後,使用以下語句匯入 FormattedMoney 和 FormattedDate。
import FormattedMoney from './FormattedMoney' import FormattedDate from './FormattedDate'
步驟 2 - 接下來,透過包含 FormattedMoney 和 FormattedDater 元件來更新 render 方法。
render() {
return (
<div>
<div><b>Item:</b> <em>{this.props.item.name}</em></div>
<div><b>Amount:</b>
<em>
<FormattedMoney value={this.props.item.amount} />
</em>
</div>
<div><b>Spend Date:</b>
<em>
<FormattedDate value={this.props.item.spendDate} />
</em>
</div>
<div><b>Category:</b>
<em>{this.props.item.category}</em></div>
</div>
);
}
在這裡,我們透過元件的值屬性傳遞了 amount 和 spendDate。
下面給出了 ExprenseEntryItem 元件的最終更新原始碼 -
import React from 'react'
import FormattedMoney from './FormattedMoney'
import FormattedDate from './FormattedDate'
class ExpenseEntryItem extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<div><b>Item:</b> <em>{this.props.item.name}</em></div>
<div><b>Amount:</b>
<em>
<FormattedMoney value={this.props.item.amount} />
</em>
</div>
<div><b>Spend Date:</b>
<em>
<FormattedDate value={this.props.item.spendDate} />
</em>
</div>
<div><b>Category:</b>
<em>{this.props.item.category}</em></div>
</div>
);
}
}
export default ExpenseEntryItem;
index.js
開啟 index.js 並透過傳遞 item 物件來呼叫 ExpenseEntryItem 元件。
const item = {
id: 1,
name : "Grape Juice",
amount : 30.5,
spendDate: new Date("2020-10-10"),
category: "Food"
}
ReactDOM.render(
<React.StrictMode>
<ExpenseEntryItem item={item} />
</React.StrictMode>,
document.getElementById('root')
);
接下來,使用 npm 命令啟動應用程式。
npm start
開啟瀏覽器,在位址列中輸入 https://:3000 並按回車鍵。
廣告