
- 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 應用中引入事件
- 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 - 地圖
- 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 - 上下文
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - 片段
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 最佳化效能
- ReactJS - Profiler API
- ReactJS - 門戶
- ReactJS - 無 ES6/ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - 渲染 Props
- ReactJS - 靜態型別檢查
- ReactJS - 嚴格模式
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - 自定義程式碼
自定義程式碼
讓我們移除應用程式的預設原始碼並引導應用程式,以便更好地理解 React 應用程式的內部機制。
刪除 src 和 public 資料夾下的所有檔案。
接下來,在 src 下建立一個名為 components 的資料夾,以包含我們的 React 元件。我們的想法是建立兩個檔案,`
應用程式的最終結構如下所示:
|-- package-lock.json |-- package.json `-- public |-- index.html `-- src |-- index.js `-- components | |-- mycom.js | |-- mycom.css
讓我們建立一個新的元件 HelloWorld 來確認我們的設定是否正常工作。在 components 資料夾下建立一個名為 HelloWorld.js 的檔案,並編寫一個簡單的元件來輸出 Hello World 訊息。
import React from "react"; class HelloWorld extends React.Component { render() { return ( <div> <h1>Hello World!</h1> </div> ); } } export default HelloWorld;
接下來,在 src 資料夾下建立我們的主檔案 index.js,並呼叫我們新建立的元件。
import React from 'react'; import ReactDOM from 'react-dom'; import HelloWorld from './components/HelloWorld'; ReactDOM.render( <React.StrictMode> <HelloWorld /> </React.StrictMode>, document.getElementById('root') );
接下來,建立一個 html 檔案 index.html(位於 public 資料夾下*),它將作為我們應用程式的入口點。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Expense Manager</title> </head> <body> <div id="root"></div> </body> </html>
執行應用程式
讓我們透過呼叫 package.json 檔案中配置的啟動指令碼執行應用程式。
> npm start
它將在本地系統中啟動應用程式,可以透過瀏覽器訪問 @ https://:3000。
> expense-manager@0.1.0 start D:\path\to\expense-manager > react-scripts start i 「wds」: Project is running at http://192.168.56.1/ i 「wds」: webpack output is served from i 「wds」: Content not from webpack is served from D:\path\to\expense-manager\public i 「wds」: 404s will fallback to / Starting the development server... Compiled successfully! You can now view expense-manager in the browser. Local: https://:3000 On Your Network: http://192.168.56.1:3000 Note that the development build is not optimized. To create a production build, use npm run build.
開啟您喜歡的瀏覽器,然後轉到 https://:3000。應用程式的結果如下所示:

使用自定義解決方案
正如我們之前瞭解到的,Create React App 是啟動 React 應用程式的推薦工具。它包含開發 React 應用程式所需的一切。但有時,應用程式不需要 Create React App 提供的所有功能,我們希望我們的應用程式更小巧簡潔。然後,我們可以使用我們自己的自定義解決方案來建立 React 應用程式,並僅包含足以支援我們應用程式的依賴項。
要建立自定義專案,我們需要具備以下四個方面的基本知識。
包管理器 - 高階應用程式管理。我們使用 npm 作為預設的包管理器。
編譯器 - 將 JavaScript 變體編譯成瀏覽器支援的標準 JavaScript。我們使用 Babel 作為預設的編譯器。
打包器 - 將多個原始檔(JavaScript、html 和 css)捆綁到一個可部署的程式碼中。Create React App 使用 webpack 作為其打包器。讓我們在接下來的部分學習如何使用 Rollup 和 Parcel 打包器。
Web 伺服器 - 啟動開發伺服器並啟動我們的應用程式。Create React App 使用內部 Web 伺服器,我們也可以使用 serve 作為我們的開發伺服器。