Skip to content

Commit

Permalink
Merge e911a61 into e968a5a
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 13, 2022
2 parents e968a5a + e911a61 commit 0cd56c3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function diff(
if (c._nextState == null) {
c._nextState = c.state;
}

if (newType.getDerivedStateFromProps != null) {
if (c._nextState == c.state) {
c._nextState = assign({}, c._nextState);
Expand All @@ -110,11 +111,6 @@ export function diff(
oldProps = c.props;
oldState = c.state;

for (tmp = 0; tmp < c._stateCallbacks.length; tmp++) {
c._renderCallbacks.push(c._stateCallbacks[tmp]);
c._stateCallbacks = [];
}

// Invoke pre-render lifecycle methods
if (isNew) {
if (
Expand Down Expand Up @@ -156,6 +152,12 @@ export function diff(
newVNode._children.forEach(vnode => {
if (vnode) vnode._parent = newVNode;
});

for (let i = 0; i < c._stateCallbacks.length; i++) {
c._renderCallbacks.push(c._stateCallbacks[i]);
}
c._stateCallbacks = [];

if (c._renderCallbacks.length) {
commitQueue.push(c);
}
Expand Down Expand Up @@ -188,6 +190,11 @@ export function diff(
if (renderHook) renderHook(newVNode);

tmp = c.render(c.props, c.state, c.context);

for (let i = 0; i < c._stateCallbacks.length; i++) {
c._renderCallbacks.push(c._stateCallbacks[i]);
}
c._stateCallbacks = [];
} else {
do {
c._dirty = false;
Expand Down Expand Up @@ -524,7 +531,11 @@ export function unmount(vnode, parentVNode, skipRemove) {
if ((r = vnode._children)) {
for (let i = 0; i < r.length; i++) {
if (r[i]) {
unmount(r[i], parentVNode, skipRemove || typeof vnode.type !== 'function');
unmount(
r[i],
parentVNode,
skipRemove || typeof vnode.type !== 'function'
);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/browser/lifecycles/componentDidMount.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { createElement, render, Component } from 'preact';
import { setupRerender } from 'preact/test-utils';
import { setupScratch, teardown } from '../../_util/helpers';

/** @jsx createElement */

describe('Lifecycle methods', () => {
/** @type {HTMLDivElement} */
let scratch;
let rerender;

beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
});

afterEach(() => {
Expand All @@ -32,5 +35,32 @@ describe('Lifecycle methods', () => {
render(<App />, scratch);
expect(spy).to.have.been.calledOnceWith(scratch.firstChild);
});

it('supports multiple setState callbacks', () => {
const spy = sinon.spy();

class App extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}

componentDidMount() {
// eslint-disable-next-line
this.setState({ count: 1 }, spy);
// eslint-disable-next-line
this.setState({ count: 2 }, spy);
}

render() {
return <div />;
}
}

render(<App />, scratch);

rerender();
expect(spy).to.have.been.calledTwice;
});
});
});
8 changes: 4 additions & 4 deletions test/browser/lifecycles/lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ describe('Lifecycle methods', () => {
'inner render',
'inner getSnapshotBeforeUpdate',
'inner componentDidUpdate',
'outer setState callback',
'outer componentDidUpdate'
'outer componentDidUpdate',
'outer setState callback'
]);

// Inner state update
Expand All @@ -168,8 +168,8 @@ describe('Lifecycle methods', () => {
'inner shouldComponentUpdate',
'inner render',
'inner getSnapshotBeforeUpdate',
'inner setState callback',
'inner componentDidUpdate'
'inner componentDidUpdate',
'inner setState callback'
]);

// Force update Outer
Expand Down

0 comments on commit 0cd56c3

Please sign in to comment.