fix: prevent infinite re-renders in Renderer component #76
+302
−59
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #53 — Renderer component causes infinite re-renders when given a static JSON tree in non-streaming scenarios.
Root Causes
The infinite re-render loop was caused by multiple interacting issues in the context providers:
1. Unstable
getcallback inDataProvideruseCallbackforgetdepended ondatastate, so every data change created a newget→ new context value → all consumers re-rendered. Fixed by using a ref to read current data, keeping callback identity stable.2. Unstable
executecallback inActionProviderexecutedepended ondata,handlers,set, andnavigatedirectly. Any data change recreatedexecute→ new context value → consumers re-render → potential cascading updates. Fixed by reading frequently-changing values from refs.3. Inline
actionHandlersobject increateRendererA new
{ __default__: ... }object was created on every render ofCatalogRenderer, causingActionProviderto receive new props each cycle. Fixed by memoizing withuseMemoand using a ref for theonActioncallback.4. Missing bail-out in
DataProvidersync effectsetData((prev) => ({ ...prev, ...initialData }))always created a new object reference even when values hadn't changed, triggering unnecessary re-renders. Added a JSON equality check to bail out when the merged result is identical.Changes
packages/react/src/contexts/data.tsx— Stabilizedgetcallback with ref; added bail-out in sync effectpackages/react/src/contexts/actions.tsx— Stabilizedexecutecallback with refs; added handler sync effectpackages/react/src/renderer.tsx— MemoizedactionHandlersincreateRendererpackages/react/src/contexts/rerender.test.tsx— Added 4 regression testsTests
All 186 tests pass (11 test files), including 4 new regression tests that verify:
getcallback identity remains stable across data changes