-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.js
60 lines (53 loc) · 1.24 KB
/
react.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
const React = {
createElement,
useState,
}
// 构造element结构 以便识别组件类型以及props
function createElement(type, props, ...children) {
return {
type,
props: {
...props,
children: children.map(child => typeof child === 'object' ? child : createTextElement(child))
}
}
}
// 创建文本节点
function createTextElement(text) {
return {
type: "TEXT_ELEMENT",
props: {
nodeValue: text,
children: []
}
}
}
// useState钩子
function useState(initial) {
// 上一次运行时的hook
const oldHook = wipFiber.alternate && wipFiber.alternate.hooks && wipFiber.alternate.hooks[hookIndex];
const hook = {
state: oldHook ? oldHook.state : initial,
queue: [],
}
// 执行hook中的action
const actions = oldHook ? oldHook.queue : [];
actions.forEach(action => {
hook.state = action(hook.state);
})
const setState = action => {
// 将action放入执行队列
hook.queue.push(action);
wipRoot = {
dom: currentRoot.dom,
props: currentRoot.props,
alternate: currentRoot,
}
// 设置下个执行单元
nextUnitOfWork = wipRoot;
deletions = [];
}
wipFiber.hooks.push(hook);
hookIndex++;
return [hook.state, setState];
}