reactjs / reactjs/react.dev

Explanation on what can go wrong if Hooks were called inside loops or nested functions?

未關閉
#2,492 2 則留言 3 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
JavaScript
星號
11.8k
分支
7.9k
平均合併
1 天 11 小時
30 天內合併 PR
11

描述

The Rules of Hooks section states that the first rule of Hooks is to only call hooks at the top level, i.e. "Don’t call Hooks inside loops, conditions, or nested functions". Later in the section, it explains what happens if we put a Hook call inside a condition. This still leaves me with some questions/doubts though. Are there examples where something can go wrong if Hooks were:

  • called inside a loop?
  • called inside a nested function? Furthermore, isn't a custom hook just a nested function? 🤔

I've tried to dig into some related questions on Stack Overflow here and here, but the answers provided only give the correct examples, but not the incorrect ones.

Could you provide incorrect examples for the other two cases? Or at least a little bit clarification would be most appreciated. :)

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Rules of Hooks 區段開始,尤其是現有對條件式 Hook 呼叫的說明。加入涵蓋迴圈和巢狀函式中的 Hooks 的錯誤範例或澄清,包括 custom Hooks 有何不同,並在文件清楚說明這些情況後視工作為完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript, react
領域
documentation
Issue 類型
文件
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。