Skip to content
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

feat: debug throw on too many rerenders #4349

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions debug/src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,31 @@ export function initDebug() {
if (oldBeforeDiff) oldBeforeDiff(vnode);
};

let renderCount = 0;
let currentComponent;
options._render = vnode => {
if (oldRender) {
oldRender(vnode);
}
hooksAllowed = true;

const nextComponent = vnode._component;
if (nextComponent === currentComponent) {
renderCount++;
} else {
renderCount = 1;
}

if (renderCount >= 25) {
throw new Error(
`Too many re-renders. This is limited to prevent an infinite loop ` +
`which may lock up your browser. The component causing this is: ${getDisplayName(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the ...which may lock up your browser. bit just to emphasize this isn't something one could safely ignore, especially as we'll only address it in dev (for most users, anyhow. I doubt many are using /debug in prod).

vnode
)}`
);
}

currentComponent = nextComponent;
};

options._hook = (comp, index, type) => {
Expand Down
32 changes: 32 additions & 0 deletions debug/test/browser/debug.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement, render, createRef, Component, Fragment } from 'preact';
import { useState } from 'preact/hooks';
import {
setupScratch,
teardown,
Expand Down Expand Up @@ -271,6 +272,37 @@ describe('debug', () => {
expect(console.error).to.not.be.called;
});

it('throws an error if a component rerenders too many times', () => {
let rerenderCount = 0;
function TestComponent({ loop = false }) {
const [count, setCount] = useState(0);
if (loop) {
setCount(count + 1);
}

if (count > 30) {
expect.fail(
'Repeated rerenders did not cause the expected error. This test is failing.'
);
}

rerenderCount += 1;
return <div />;
}

expect(() => {
render(
<Fragment>
<TestComponent />
<TestComponent loop />
</Fragment>,
scratch
);
}).to.throw(/Too many re-renders/);
// 1 for first TestComponent + 24 for second TestComponent
expect(rerenderCount).to.equal(25);
});

describe('duplicate keys', () => {
const List = props => <ul>{props.children}</ul>;
const ListItem = props => <li>{props.children}</li>;
Expand Down
Loading