Best Practices/Software Architecture
Dan Abramov·Jan 26, 2019·8 min readBy Dan Abramov
AngularJavaScriptSoftware ArchitectureReactAPI Design
AI summary
This article explains the design principles behind the React Hooks API, specifically why certain features like React.memo or Context.Provider are not implemented as Hooks. By evaluating APIs through the lenses of composition and debuggability, it demonstrates how some potential Hooks would create fragile code and complex, non-linear debugging paths.
Key points
- React APIs are designed to preserve component composition so custom Hooks do not conflict with one another.
- Debuggability is a core constraint; API design must ensure that developers can trace state or prop errors by walking up the component tree.
- Implementing APIs like `useBailout()` as hooks would break composition and significantly increase the cognitive load required to debug state changes.
- Hooks are suitable for logic and state management, but declarative structures that control component lifecycle rendering are often better served by components or specialized APIs.