-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (146 loc) · 4.29 KB
/
index.js
File metadata and controls
178 lines (146 loc) · 4.29 KB
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
function getCursorPosition(id) {
const input = document.getElementById(id);
const cursorPosition = input.selectionStart;
return cursorPosition;
}
function setCursorPosition(id, cursorPosition) {
const input = document.getElementById(id);
input.focus();
input.setSelectionRange(cursorPosition, cursorPosition);
}
const ReactDOM = (function () {
let _container;
let _Component;
return {
update() {
this.render(_container, _Component);
},
render(container, Component) {
_container = container;
_Component = Component;
const componentDOM = React.render(Component);
container.replaceChildren();
container.appendChild(componentDOM);
},
};
})();
const React = (function () {
let hooks = [];
let currentIndex = 0;
return {
render(Component) {
currentIndex = 0;
const Comp = Component();
return Comp;
},
useState(initialValue) {
const useStateIndex = currentIndex;
currentIndex++;
hooks[useStateIndex] = hooks[useStateIndex] ?? initialValue;
const setState = (newVal) => {
const newState =
typeof newVal === "function" ? newVal(hooks[useStateIndex]) : newVal;
hooks[useStateIndex] = newState;
ReactDOM.update();
};
return [hooks[useStateIndex], setState];
},
useReducer(reducer, initialState) {
const useReducerIndex = currentIndex;
currentIndex++;
hooks[useReducerIndex] = hooks[useReducerIndex] ?? initialState;
const dispatch = (action) => {
const newState = reducer(hooks[useReducerIndex], action);
hooks[useReducerIndex] = newState;
ReactDOM.update();
};
return [hooks[useReducerIndex], dispatch];
},
useEffect(callback, depArray) {
const hasNoDeps = !depArray;
const deps = hooks[currentIndex];
const hasChangedDeps = deps
? !depArray.every((el, i) => el === deps[i])
: true;
if (hasNoDeps || hasChangedDeps) {
hooks[currentIndex] = depArray;
callback();
}
currentIndex++;
},
};
})();
function counterReducer(prevState, action) {
switch (action.type) {
case "increase": {
return {
count: prevState.count + 1,
};
}
case "decrease": {
return {
count: prevState.count - 1,
};
}
default: {
return prevState;
}
}
}
function Counter() {
const [state, dispatch] = React.useReducer(
counterReducer,
localStorage.getItem("state")
? JSON.parse(localStorage.getItem("state"))
: { count: 0 }
);
React.useEffect(() => {
localStorage.setItem("state", JSON.stringify(state));
}, [state]);
const textDisplay = document.createElement("p");
textDisplay.textContent = state.count;
const buttonIncrease = document.createElement("button");
buttonIncrease.textContent = "+";
buttonIncrease.onclick = () => dispatch({ type: "increase" });
const buttonDecrease = document.createElement("button");
buttonDecrease.textContent = "-";
buttonDecrease.onclick = () => dispatch({ type: "decrease" });
const container = document.createElement("div");
container.appendChild(textDisplay);
container.appendChild(buttonIncrease);
container.appendChild(buttonDecrease);
return container;
}
function Input() {
const [inputValue, setInputValue] = React.useState(
localStorage.getItem("inputValue") ?? ""
);
React.useEffect(() => {
localStorage.setItem("inputValue", inputValue);
}, [inputValue]);
function handleChange(event) {
const cursorPosition = getCursorPosition("input");
setInputValue(event.target.value);
setCursorPosition("input", cursorPosition);
}
const input = document.createElement("input");
input.id = "input";
input.value = inputValue;
input.oninput = handleChange;
const display = document.createElement("p");
display.textContent = inputValue;
const container = document.createElement("div");
container.appendChild(input);
container.appendChild(display);
return container;
}
function App() {
const counter = Counter();
const input = Input();
const appContainer = document.createElement("div");
appContainer.appendChild(counter);
appContainer.appendChild(input);
return appContainer;
}
const root = document.getElementById("root");
ReactDOM.render(root, App);