Skip to content

Commit

Permalink
fix isolated rerenders (#4382)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed May 11, 2024
1 parent 6449692 commit cece8c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions debug/src/debug.js
Expand Up @@ -353,6 +353,10 @@ export function initDebug() {
});
}

if (vnode._component === currentComponent) {
renderCount = 0;
}

if (
typeof type === 'string' &&
(isTableElement(type) ||
Expand Down
19 changes: 19 additions & 0 deletions debug/test/browser/debug.test.js
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../../test/_util/helpers';
import './fakeDevTools';
import 'preact/debug';
import { setupRerender } from 'preact/test-utils';
import * as PropTypes from 'prop-types';

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

beforeEach(() => {
errors = [];
warnings = [];
scratch = setupScratch();
rerender = setupRerender();
sinon.stub(console, 'error').callsFake(e => errors.push(e));
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
});
Expand Down Expand Up @@ -303,6 +306,22 @@ describe('debug', () => {
expect(rerenderCount).to.equal(25);
});

it('does not throw an error if a component renders many times in different cycles', () => {
let set;
function TestComponent() {
const [count, setCount] = useState(0);
set = () => setCount(count + 1);
return <div>{count}</div>;
}

render(<TestComponent />, scratch);
for (let i = 0; i < 30; i++) {
set();
rerender();
}
expect(scratch.innerHTML).to.equal('<div>30</div>');
});

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

0 comments on commit cece8c3

Please sign in to comment.