-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
183 lines (155 loc) · 3.78 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const __DEV__ = false;
let REACT_ELEMENT_TYPE = 0xeac7;
let REACT_PORTAL_TYPE = 0xeaca;
let REACT_FRAGMENT_TYPE = 0xeacb;
let REACT_STRICT_MODE_TYPE = 0xeacc;
let REACT_PROFILER_TYPE = 0xead2;
let REACT_PROVIDER_TYPE = 0xeacd;
let REACT_CONTEXT_TYPE = 0xeace;
let REACT_FORWARD_REF_TYPE = 0xead0;
let REACT_SUSPENSE_TYPE = 0xead1;
let REACT_SUSPENSE_LIST_TYPE = 0xead8;
let REACT_MEMO_TYPE = 0xead3;
let REACT_LAZY_TYPE = 0xead4;
let REACT_BLOCK_TYPE = 0xead9;
let REACT_SERVER_BLOCK_TYPE = 0xeada;
let REACT_FUNDAMENTAL_TYPE = 0xead5;
let REACT_RESPONDER_TYPE = 0xead6;
let REACT_SCOPE_TYPE = 0xead7;
let REACT_OPAQUE_ID_TYPE = 0xeae0;
let REACT_DEBUG_TRACING_MODE_TYPE = 0xeae1;
let REACT_OFFSCREEN_TYPE = 0xeae2;
let REACT_LEGACY_HIDDEN_TYPE = 0xeae3;
class Main extends Component {
wrapper = null;
state = {
h1: "Hello",
h2: "React",
};
// static getDerivedStateFromProps(nextProps, prevState) {
// // console.log("prevState ->", prevState);
// }
componentWillUpdate() {
console.log("Main will update");
}
componentDidMount() {
setTimeout(() => {
this.reverse();
}, 1000);
}
reverse = () => {
this.setState((state) => ({
h1: state.h1.split("").reverse().join(""),
h2: state.h2.split("").reverse().join(""),
}));
};
render() {
return createElement(
"div",
{
className: "wrapper",
ref: (ref) => (this.wrapper = ref),
key: "main",
},
createElement("h1", null, this.state.h1),
createElement("h2", null, this.state.h2),
createElement("button", { onClick: this.reverse }, "click")
);
}
}
function HooksComponent() {
useEffect(() => {}, []);
}
const hasOwnProperty = Object.prototype.hasOwnProperty;
const RESERVED_PROPS = {
key: true,
ref: true,
__self: true,
__source: true,
};
// important
const ReactCurrentOwner = {
current: null,
};
function createElement(type, config, children) {
let propName;
const props = {};
let key = null;
let ref = null;
let self = null;
let source = null;
if (config != null) {
if (config.ref) {
ref = config.ref;
}
if (config.key) {
key = config.key;
}
self = config.__self === undefined ? null : config.__self;
source = config.__source === undefined ? null : config.__source;
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
props[propName] = config[propName];
}
}
}
const childrenLength = arguments.length - 2;
if (childrenLength === 1) {
props.children = children;
} else if (childrenLength > 1) {
const childArray = Array(childrenLength);
for (let i = 0; i < childrenLength; i++) {
childArray[i] = arguments[i + 2];
}
props.children = childArray;
}
if (type && type.defaultProps) {
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
}
}
}
return ReactElement(
type,
key,
ref,
self,
source,
ReactCurrentOwner.current,
props
);
}
function ReactElement(type, key, ref, self, source, owner, props) {
const element = {
$$typeof: REACT_ELEMENT_TYPE,
type: type,
key: key,
ref: ref,
props: props,
_owner: owner,
};
return element;
}
const element1 = createElement(function () {
useEffect(() => {
console.log("did mount");
}, []);
return createElement(
"div",
{
className: "wrapper",
},
createElement("h1", null, "Hello"),
createElement("h2", null, "React")
);
});
const element2 = createElement(Main);
const body = document.querySelector("#body");
// fixme
// createRoot(body).render(element2);
render(element1, body);