How Does setState Know What to Do? — overreacted
13 hours ago
- The `react` package only provides APIs for defining components, while the actual implementation (e.g., DOM updates) is handled by renderers like `react-dom`.
- `setState` in `React.Component` does not directly update the DOM; it delegates to a renderer-specific `updater` field set on the component instance.
- Hooks like `useState` use a similar pattern with a `dispatcher` object, which is set by the renderer before rendering a component.
- This separation allows React to work across different platforms (DOM, React Native, etc.) without changing component code, and it is a form of dependency injection.