Skip to content

Commit

Permalink
Merge branch 'master' into skip-coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed Mar 9, 2019
2 parents 237f7e0 + 4f671e7 commit d3ec0e7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion debug/mangle.json
Expand Up @@ -8,7 +8,8 @@
"properties": {
"regex": "^_",
"reserved": [
"__REACT_DEVTOOLS_GLOBAL_HOOK__"
"__REACT_DEVTOOLS_GLOBAL_HOOK__",
"_renderers"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/component.js
Expand Up @@ -108,7 +108,7 @@ let q = [];
* Asynchronously schedule a callback
* @type {(cb) => void}
*/
const defer = typeof Promise=='function' ? Promise.resolve().then.bind(Promise.resolve()) : setTimeout;
const defer = typeof Promise=='function' ? Promise.prototype.then.bind(Promise.resolve()) : setTimeout;

/*
* The value of `Component.debounce` must asynchronously invoke the passed in callback. It is
Expand Down
17 changes: 2 additions & 15 deletions src/diff/children.js
Expand Up @@ -23,8 +23,8 @@ export function diffChildren(parentDom, newParentVNode, oldParentVNode, context,
nextDom, sibDom, focus,
childDom;

let newChildren = getVNodeChildren(newParentVNode);
let oldChildren = oldParentVNode==null || oldParentVNode==EMPTY_OBJ ? EMPTY_ARR : getVNodeChildren(oldParentVNode);
let newChildren = newParentVNode._children || toChildArray(newParentVNode.props.children, newParentVNode._children=[], coerceToVNode);
let oldChildren = oldParentVNode!=null && oldParentVNode!=EMPTY_OBJ && oldParentVNode._children || EMPTY_ARR;

let oldChildrenLength = oldChildren.length;

Expand Down Expand Up @@ -123,19 +123,6 @@ export function diffChildren(parentDom, newParentVNode, oldParentVNode, context,
for (i=oldChildren.length; i--; ) if (oldChildren[i]!=null) unmount(oldChildren[i], ancestorComponent);
}

/**
* Get the children of a virtual node as a flat array
* @param {import('../internal').VNode} vnode The virtual node to get the
* children of
* @returns {Array<import('../internal').VNode>} The virtual node's children
*/
function getVNodeChildren(vnode) {
if (vnode._children==null) {
toChildArray(vnode.props.children, vnode._children=[], coerceToVNode);
}
return vnode._children;
}

/**
* Flatten a virtual nodes children to a single dimensional array
* @param {import('../index').ComponentChildren} children The unflattened
Expand Down
11 changes: 9 additions & 2 deletions src/index.d.ts
Expand Up @@ -99,8 +99,15 @@ declare namespace preact {
static displayName?: string;
static defaultProps?: any;
static contextType?: PreactContext<any>;
static getDerivedStateFromProps?<P, S>(props: P, state: S): Partial<S>;
static getDerivedStateFromError?<S>(error: any): Partial<S>;

// Static members cannot reference class type parameters. This is not
// supported in TypeScript. Reusing the same type arguments from `Component`
// will lead to an impossible state where one cannot satisfy the type
// constraint under no circumstances, see #1356.In general type arguments
// seem to be a bit buggy and not supported well at the time of this
// writing with TS 3.3.3333.
static getDerivedStateFromProps?(props: Readonly<object>, state: Readonly<object>): object;
static getDerivedStateFromError?(error: any): object;

state: Readonly<S>;
props: RenderableProps<P>;
Expand Down
19 changes: 19 additions & 0 deletions test/ts/Component-test.tsx
Expand Up @@ -97,6 +97,25 @@ class RandomChildrenComponent extends Component<RandomChildrenComponenProps> {
}
}

class StaticComponent extends Component<SimpleComponentProps, SimpleState> {
static getDerivedStateFromProps(props: SimpleComponentProps, state: SimpleState): Partial<SimpleState> {
return {
...props,
...state
}
}

static getDerivedStateFromError(err: Error) {
return {
name: err.message
};
}

render() {
return null;
}
}

describe("Component", () => {
const component = new SimpleComponent({ initialName: "da name" });

Expand Down

0 comments on commit d3ec0e7

Please sign in to comment.