This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
generated from kernel-addons/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-utils.js
58 lines (46 loc) · 1.49 KB
/
react-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
let elementSymbol = null;
export const setElementSymbol = function (symbol) {
elementSymbol = symbol;
};
export const createElement = function (type, props = {}, ...children) {
const element = {
$$typeof: elementSymbol,
type: type,
ref: null,
props: {
children: children
}
};
for (const prop in props) {
switch (prop) {
case "key":
case "ref":
element[prop] ??= props[prop];
break;
case "children":
element.props.children.push(...(Array.isArray(props[prop]) ? props[prop] : [props[prop]]));
break;
default:
element.props[prop] = props[prop];
}
}
if (type?.defaultProps) {
for (const prop in type.defaultProps) {
element.props[prop] ??= type.defaultProps[prop];
}
}
return element;
};
export class ReactComponent {
constructor(props) {
this.props = props;
}
isMounted() {return !!this._isMounted;}
forceUpdate(callback) {this.updater.enqueueForceUpdate(this, callback, "forceUpdate");}
isReactComponent() {return true;}
replaceState(state, callback) {this.updater.enqueueReplaceState(this, callback, state);}
setState(state, callback) {
if (typeof state !== "object" && typeof state !== "function" && state != null) throw "Silly.";
this.updater.enqueueSetState(this, state, callback, "setState");
}
}