雖然沒準備什麼資料
  • JS魔法學院
    • JS表單
    • 教程
    • 練習題
  • AI
  • 程式相關
    • CSS
    • Javascript
  • 試題與解答
    • 微生物免疫
    • 化學
    • 生物輸送
  • 台式外語
    • 韓國語
    • 日本語
  • 有趣網站
首頁 » React useRef × BootStrap Modal 筆記
BootStrapReact教學

React useRef × BootStrap Modal 筆記

by Jamic 2026-01-14
114

一、useRef 是什麼(What)

useRef 是 React 提供的一個 Hook,用來建立一個跨 render 穩定存在的容器物件。

const ref = useRef(initialValue)
// ref = { current: initialValue }
  • 真正的值永遠放在 ref.current
  • 修改 ref.current 不會觸發 re-render

二、為什麼需要 useRef(Why)

React function component 每次 render 都會重新執行,
一般變數(let / const)無法跨 render 保留值。

useRef 解決的問題是:

我要記住一個值,但它不屬於 UI 狀態,也不該影響畫面

常見用途:

  • 操作 DOM
  • 儲存第三方套件 instance
  • timer / interval id
  • 記錄前一次的值
  • flag(是否初始化過)

三、useRef 的核心概念(一定要記住)

useRef 回傳的是「容器(盒子)」
真正的值在 ref.current 裡

❌ ref 不是值本身
✅ ref.current 才是值


四、為什麼不能直接用 ref,而一定要 .current?

useRef 實際結構

const modalRef = useRef(null)

等價於:

const modalRef = { current: null }
  • React 只保證 這個物件本身永遠不變
  • current 可以自由修改,不影響 render

五、React + Bootstrap Modal 的角色分工

資料分類

類型用什麼
UI 顯示資料useState
DOM 存取useRef
Bootstrap 控制器useRef

六、modalRef 與 modalInstanceRef 分別是什麼?

1️⃣ modalRef.current

<div className="modal" ref={modalRef}></div>
  • modalRef.current = 實際的 DOM 元素(HTMLDivElement)
  • 由 React 自動塞入
  • 用來交給 Bootstrap 接管

2️⃣ modalInstanceRef.current

modalInstanceRef.current = new Modal(modalRef.current)
  • modalInstanceRef.current = Bootstrap Modal instance(控制器物件)
  • 包含 .show()、.hide()、.dispose()
  • 屬於 imperative 行為,不是 UI state

七、這行程式碼在做什麼?

modalInstanceRef.current = new Modal(modalRef.current)

拆解:

  1. modalRef.current
    → 取得 modal 的 DOM
  2. new Modal(dom)
    → 交給 Bootstrap 建立 Modal 控制器
    (處理動畫、backdrop、ESC、focus trap)
  3. 存進 modalInstanceRef.current
    → 之後可重複呼叫 .show() / .hide()

八、為什麼 modalInstance 不能用 useState?

❌ 不適合用 useState 的原因:

  • instance 改變不影響 UI
  • 會造成不必要的 re-render
  • 語意錯誤(控制器 ≠ UI 狀態)

❌ 不能用一般變數:

  • render 會重置
  • 會失去 instance

✅ useRef 剛好符合:

  • 跨 render
  • 不影響畫面
  • 專門給行為用

九、最小實戰範例(概念版)

const modalRef = useRef(null)
const modalInstanceRef = useRef(null)

const openModal = () => {
  modalInstanceRef.current = new Modal(modalRef.current)
  modalInstanceRef.current.show()
}

const closeModal = () => {
  modalInstanceRef.current.hide()
}

十、useRef 是否違反「資料驅動畫面」?

❌ 沒有違反

正確理解是:

資料驅動畫面 → 只適用於 UI state

useRef 管的是:

  • 非畫面資料
  • imperative 行為
  • 與現實世界(DOM / 套件)的橋樑

十一、快速判斷法則(實戰心法)

問自己一句話:

這個值改變,需要讓畫面更新嗎?

  • 是 → useState
  • 否 → useRef

十二、終極總結(一句話)

useState 管畫面
useRef 管行為
ref 是地址,ref.current 是內容


探索更多來自 雖然沒準備什麼資料 的內容

訂閱即可透過電子郵件收到最新文章。

bootstrapreact
分享 0 FacebookTwitterLINEEmail

留個言 取消回覆

將姓名、email存到這個瀏覽器,以便下次使用

使用電子郵件訂閱網站

輸入你的電子郵件地址訂閱網站的新文章,使用電子郵件接收新通知。

最新文章

  • React useRef × BootStrap Modal 筆記

    2026-01-14
  • 7-11 雙蔬鮪魚飯糰

    2025-10-30
  • 保持專注的秘訣

    2025-10-29
  • Cursor Zeabur 插件 vs GitHub 綁定部署

    2025-07-30
  • Product Brief Template — AI 建構式應用產品

    2025-07-10

文章分類

  • JS魔法學院 (7)
    • JS表單 (1)
    • 教程 (4)
    • 練習題 (2)
  • React教學 (1)
    • BootStrap (1)
  • soledad佈景主題 (1)
    • 單篇文章 (1)
  • Vue3學院 (2)
    • watch (1)
    • 設定 (1)
  • Wordpress外掛介紹 (1)
  • 台式外語 (5)
    • 日本語 (3)
    • 英語 (1)
    • 韓國語 (1)
  • 外食成份 (1)
  • 好書介紹 (3)
  • 小孩相關 (1)
  • 投資理財 (11)
    • 定存 (2)
    • 股票 (9)
      • 台股 (1)
      • 美股 (3)
  • 效率 (1)
  • 有趣網站 (1)
  • 未分類 (1)
  • 程式相關 (11)
    • AI (5)
    • CSS (1)
    • google sheets (1)
    • Javascript (1)
    • 豆知識 (2)
  • 網站部署 (1)
  • 試題與解答 (12)
    • 儀器分析 (1)
    • 專利研究 (2)
    • 微生物免疫 (8)
    • 醫用統計學 (1)

網站統計

  • 0
  • 7
  • 7
  • 192
  • 6,428
  • 4,587
  • 59
  • 2026-01-14

© 2023 雖然沒準備什麼資料

雖然沒準備什麼資料
  • JS魔法學院
    • JS表單
    • 教程
    • 練習題
  • AI
  • 程式相關
    • CSS
    • Javascript
  • 試題與解答
    • 微生物免疫
    • 化學
    • 生物輸送
  • 台式外語
    • 韓國語
    • 日本語
  • 有趣網站