從 React Hook 原始碼的實作與資料結構,探討為何 Hooks 須在最頂層呼叫
React 官方文件中,有條規則明確寫著 "Only call Hooks at the top level",延伸來說有不少使用 Hooks 的注意事項,例如:不要在 if/else conditions 或 loop 中使用 Hooks。然而,為什麼不能在 conditions, loop 等等情況中呼叫 Hooks 呢?這背後其實與 React Hooks 的資料結構有關,本文將試著閱讀 React Hooks 原始碼來探討這項規則的原因。