Third Rule of Hooks: "Side effects may only be caused in useEffect and event handlers"
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
I'd like to propose a third rule of hooks.
In the section "Advanced Guides > Strict Mode" it states that
Because the above methods might be called more than once, it’s important that they do not contain side-effects. Ignoring this rule can lead to a variety of problems, including memory leaks and invalid application state.
If I'm not mistaken, the inverse of this statement is that side effects based on a certain state of states shall be triggered in a useEffect, and that side effects from user interaction shall be triggered directly in the event handler. Everything else shall be side effect free. I think that adding this to the "rules of hooks" section makes it easier to remember and gives it much more visibility. I think the "variety of problems" will especially grow when concurrent mode is introduced. By having a "checklist" that can be used during code reviews etc. makes it easier to avoid this kind of problems.
Also one could maybe repeat the "Do not mutate state" rule there, cause this is still a very common problem and having it multiple times in the docs does no harm in my eyes.
What do you think?
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從連結的 Strict Mode 指南和 Rules of Hooks 區段開始。將提議的副作用指引和重複狀態突變規則與現有文件進行比較;當措辭和放置位置達成共識,且相關文件已更新時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100