-
Notifications
You must be signed in to change notification settings - Fork 658
fix: prevent infinite re-renders in Renderer component #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liuxiaopai-ai
wants to merge
1
commit into
vercel-labs:main
Choose a base branch
from
liuxiaopai-ai:fix/renderer-infinite-rerender
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−59
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import React, { useRef, useEffect } from "react"; | ||
| import { render, act } from "@testing-library/react"; | ||
| import { DataProvider, useData } from "./data"; | ||
| import { VisibilityProvider } from "./visibility"; | ||
| import { ActionProvider } from "./actions"; | ||
|
|
||
| /** | ||
| * Regression test for infinite re-render bug (Issue #53). | ||
| * | ||
| * The Renderer component (and its context providers) caused infinite | ||
| * re-renders when given a static spec in a non-streaming scenario. | ||
| * | ||
| * Root causes: | ||
| * 1. DataProvider.get depended on `data`, causing cascading memo invalidation | ||
| * 2. ActionProvider.execute depended on `data` and `handlers` directly, | ||
| * so every data change recreated the execute callback → new context value | ||
| * 3. createRenderer created a new `actionHandlers` object on every render | ||
| */ | ||
|
|
||
| /** Helper component that counts how many times it renders */ | ||
| function RenderCounter({ onRender }: { onRender: (count: number) => void }) { | ||
| const countRef = useRef(0); | ||
| countRef.current += 1; | ||
|
|
||
| useEffect(() => { | ||
| onRender(countRef.current); | ||
| }); | ||
|
|
||
| return React.createElement( | ||
| "div", | ||
| { "data-testid": "counter" }, | ||
| `renders: ${countRef.current}`, | ||
| ); | ||
| } | ||
|
|
||
| /** Helper that reads from DataProvider to subscribe to context changes */ | ||
| function DataConsumer({ onRender }: { onRender: (count: number) => void }) { | ||
| const { data } = useData(); | ||
| const countRef = useRef(0); | ||
| countRef.current += 1; | ||
|
|
||
| useEffect(() => { | ||
| onRender(countRef.current); | ||
| }); | ||
|
|
||
| return React.createElement("div", null, JSON.stringify(data)); | ||
| } | ||
|
|
||
| describe("Infinite re-render prevention", () => { | ||
| it("DataProvider does not cause re-renders when initialData reference changes but value is the same", async () => { | ||
| const renderCounts: number[] = []; | ||
| const onRender = (count: number) => { | ||
| renderCounts.push(count); | ||
| }; | ||
|
|
||
| const staticData = { user: { name: "John" } }; | ||
|
|
||
| const { rerender } = render( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: staticData }, | ||
| React.createElement(DataConsumer, { onRender }), | ||
| ), | ||
| ); | ||
|
|
||
| // Re-render with a new object reference but same values | ||
| await act(async () => { | ||
| rerender( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: { user: { name: "John" } } }, | ||
| React.createElement(DataConsumer, { onRender }), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| // Should render at most twice (initial + rerender from parent), | ||
| // NOT keep growing indefinitely | ||
| const lastCount = renderCounts[renderCounts.length - 1]; | ||
| expect(lastCount).toBeLessThanOrEqual(3); | ||
| }); | ||
|
|
||
| it("DataProvider with empty initialData does not trigger infinite updates", async () => { | ||
| const renderCounts: number[] = []; | ||
| const onRender = (count: number) => { | ||
| renderCounts.push(count); | ||
| }; | ||
|
|
||
| const { rerender } = render( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: {} }, | ||
| React.createElement(DataConsumer, { onRender }), | ||
| ), | ||
| ); | ||
|
|
||
| // Re-render with a new empty object reference | ||
| await act(async () => { | ||
| rerender( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: {} }, | ||
| React.createElement(DataConsumer, { onRender }), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const lastCount = renderCounts[renderCounts.length - 1]; | ||
| expect(lastCount).toBeLessThanOrEqual(3); | ||
| }); | ||
|
|
||
| it("full provider stack does not cause infinite re-renders with static data", async () => { | ||
| const renderCounts: number[] = []; | ||
| const onRender = (count: number) => { | ||
| renderCounts.push(count); | ||
| }; | ||
|
|
||
| const staticData = { items: [1, 2, 3] }; | ||
|
|
||
| const tree = React.createElement( | ||
| DataProvider, | ||
| { initialData: staticData }, | ||
| React.createElement( | ||
| VisibilityProvider, | ||
| null, | ||
| React.createElement( | ||
| ActionProvider, | ||
| { handlers: {} }, | ||
| React.createElement(RenderCounter, { onRender }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const { rerender } = render(tree); | ||
|
|
||
| // Re-render the entire tree with same-value data but new references | ||
| await act(async () => { | ||
| rerender( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: { items: [1, 2, 3] } }, | ||
| React.createElement( | ||
| VisibilityProvider, | ||
| null, | ||
| React.createElement( | ||
| ActionProvider, | ||
| { handlers: {} }, | ||
| React.createElement(RenderCounter, { onRender }), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| // With the fix, render count should stabilize (not grow unbounded) | ||
| const lastCount = renderCounts[renderCounts.length - 1]; | ||
| expect(lastCount).toBeLessThanOrEqual(4); | ||
| }); | ||
|
|
||
| it("DataProvider.get callback identity is stable across data changes", async () => { | ||
| const getCallbacks: Array<(path: string) => unknown> = []; | ||
|
|
||
| function GetCollector() { | ||
| const { get, set } = useData(); | ||
| const isFirst = useRef(true); | ||
|
|
||
| getCallbacks.push(get); | ||
|
|
||
| useEffect(() => { | ||
| if (isFirst.current) { | ||
| isFirst.current = false; | ||
| // Trigger a data change — `get` should NOT change identity | ||
| set("/foo", "bar"); | ||
| } | ||
| }, [set]); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| render( | ||
| React.createElement( | ||
| DataProvider, | ||
| { initialData: {} }, | ||
| React.createElement(GetCollector), | ||
| ), | ||
| ); | ||
|
|
||
| // Wait for effects | ||
| await act(async () => {}); | ||
|
|
||
| // `get` should have the same reference across renders | ||
| expect(getCallbacks.length).toBeGreaterThanOrEqual(2); | ||
| expect(getCallbacks[0]).toBe(getCallbacks[1]); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new synchronization effect only updates state when
initialHandlershas keys, so if the parent later passes{}/undefined(for example,createRenderertogglingonActionoff), the previous handler map is retained indefinitely. In that state,executestill sees actions as registered and can continue running downstreamonSuccess/onErrorbehavior, so disabling handlers no longer actually disables action execution. The sync path should also clearhandlerswhen the prop becomes empty.Useful? React with 👍 / 👎.