Skip to content

Commit

Permalink
Move devtools prevState injection into DevTools.
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Mar 10, 2019
1 parent 47fdd6d commit 8529554
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 13 additions & 1 deletion debug/src/devtools/index.js
@@ -1,4 +1,4 @@
import { options, Fragment } from 'preact';
import { options, Component, Fragment } from 'preact';
import { Renderer } from './renderer';

/**
Expand Down Expand Up @@ -152,6 +152,18 @@ export function initDevTools() {
if (prevBeforeUnmount!=null) prevBeforeUnmount(vnode);
onCommitUnmount(vnode);
});

// Inject tracking into setState
const setState = Component.prototype.setState;
Component.prototype.setState = function(update, callback) {
// Duplicated in setState() but doesn't matter due to the guard.
let s = (this._nextState!==this.state && this._nextState) || (this._nextState = Object.assign({}, this.state));

// Needed in order to check if state has changed after the tree has been committed:
this._prevState = Object.assign({}, s);

return setState.call(this, update, callback);
};
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/component.js
Expand Up @@ -42,10 +42,6 @@ Component.prototype.setState = function(update, callback) {
// only clone state when copying to nextState the first time.
let s = (this._nextState!==this.state && this._nextState) || (this._nextState = assign({}, this.state));

// Needed for the devtools to check if state has changed after the tree
// has been committed
this._prevState = assign({}, s);

// if update() mutates state in-place, skip the copy:
if (typeof update!=='function' || (update = update(s, this.props))) {
assign(s, update);
Expand Down

0 comments on commit 8529554

Please sign in to comment.