Skip to content

Commit cece8c3

Browse files
authored
fix isolated rerenders (#4382)
1 parent 6449692 commit cece8c3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

debug/src/debug.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ export function initDebug() {
353353
});
354354
}
355355

356+
if (vnode._component === currentComponent) {
357+
renderCount = 0;
358+
}
359+
356360
if (
357361
typeof type === 'string' &&
358362
(isTableElement(type) ||

debug/test/browser/debug.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '../../../test/_util/helpers';
88
import './fakeDevTools';
99
import 'preact/debug';
10+
import { setupRerender } from 'preact/test-utils';
1011
import * as PropTypes from 'prop-types';
1112

1213
// eslint-disable-next-line no-duplicate-imports
@@ -20,11 +21,13 @@ describe('debug', () => {
2021
let scratch;
2122
let errors = [];
2223
let warnings = [];
24+
let rerender;
2325

2426
beforeEach(() => {
2527
errors = [];
2628
warnings = [];
2729
scratch = setupScratch();
30+
rerender = setupRerender();
2831
sinon.stub(console, 'error').callsFake(e => errors.push(e));
2932
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
3033
});
@@ -303,6 +306,22 @@ describe('debug', () => {
303306
expect(rerenderCount).to.equal(25);
304307
});
305308

309+
it('does not throw an error if a component renders many times in different cycles', () => {
310+
let set;
311+
function TestComponent() {
312+
const [count, setCount] = useState(0);
313+
set = () => setCount(count + 1);
314+
return <div>{count}</div>;
315+
}
316+
317+
render(<TestComponent />, scratch);
318+
for (let i = 0; i < 30; i++) {
319+
set();
320+
rerender();
321+
}
322+
expect(scratch.innerHTML).to.equal('<div>30</div>');
323+
});
324+
306325
describe('duplicate keys', () => {
307326
const List = props => <ul>{props.children}</ul>;
308327
const ListItem = props => <li>{props.children}</li>;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy