Skip to content

Commit 8a730a6

Browse files
authoredMay 25, 2019
(fix) - support react-hot-loader (preactjs#1644)
* fix: use constructor instead of a _self reference this reenables react hot loader * chore: update comments, typings and mangle * chore: remove eslint-disable
1 parent a4bb1a2 commit 8a730a6

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed
 

‎mangle.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"$_context": "__n",
3434
"$_defaultValue": "__p",
3535
"$_id": "__c",
36-
"$_parentDom": "__P",
37-
"$_self": "_"
36+
"$_parentDom": "__P"
3837
}
3938
}
40-
}
39+
}

‎src/create-element.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export function createVNode(type, props, key, ref) {
6060
_children: null,
6161
_dom: null,
6262
_lastDomChild: null,
63-
_component: null
63+
_component: null,
64+
constructor: undefined
6465
};
65-
vnode._self = vnode;
6666

6767
if (options.vnode) options.vnode(vnode);
6868

‎src/diff/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ export function diff(parentDom, newVNode, oldVNode, context, isSvg, excessDomChi
3535
newType = newVNode.type, clearProcessingException;
3636

3737
// When passing through createElement it assigns the object
38-
// ref on _self, to prevent JSON Injection we check if this attribute
39-
// is equal.
40-
if (newVNode._self!==newVNode) return null;
38+
// constructor as undefined. This to prevent JSON-injection.
39+
if (newVNode.constructor !== undefined) return null;
4140

4241
if (tmp = options.diff) tmp(newVNode);
4342

@@ -382,7 +381,7 @@ function catchErrorInComponent(error, component) {
382381
continue;
383382
}
384383
}
385-
else if (component.constructor.getDerivedStateFromError!=null) {
384+
else if (component.constructor && component.constructor.getDerivedStateFromError!=null) {
386385
component.setState(component.constructor.getDerivedStateFromError(error));
387386
}
388387
else if (component.componentDidCatch!=null) {

‎src/internal.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export interface PreactElement extends HTMLElement {
2525
export interface VNode<P = {}> extends preact.VNode<P> {
2626
// Redefine type here using our internal ComponentFactory type
2727
type: string | ComponentFactory<P> | null;
28-
_self: this;
2928
_children: Array<VNode> | null;
3029
/**
3130
* The [first (for Fragments)] DOM child of a VNode

‎test/shared/createElement.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ describe('createElement(jsx)', () => {
3232
expect(<Test />).to.have.property('type', Test);
3333
});
3434

35-
it('should set VNode._self property to prevent json injection', () => {
35+
it('should set VNode.constructor property to prevent json injection', () => {
3636
const vnode = <span />;
37-
expect(vnode._self).to.equal(vnode);
37+
expect(vnode.constructor).to.equal(undefined);
3838
});
3939

4040
it('should set VNode#props property', () => {
@@ -68,7 +68,7 @@ describe('createElement(jsx)', () => {
6868
});
6969

7070
it('should have ordered VNode properties', () => {
71-
expect(Object.keys(<div />).filter(key => !/^_/.test(key))).to.deep.equal(['type', 'props', 'key', 'ref']);
71+
expect(Object.keys(<div />).filter(key => !/^_/.test(key))).to.deep.equal(['type', 'props', 'key', 'ref', 'constructor']);
7272
});
7373

7474
it('should preserve raw props', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.