-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
132 lines (112 loc) · 3.38 KB
/
Copy pathindex.tsx
File metadata and controls
132 lines (112 loc) · 3.38 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
const React = {
createElement: (tag, props, ...children) => {
if (typeof tag == "function") {
try {
return tag(props);
} catch ({ promise, key }) {
promise.then((data) => {
promiseCache.set(key, data);
reRender();
});
return { tag: "h1", props: { children: ["...Loading"] } };
}
}
const element = { tag, props: { ...props, children } };
return element;
},
};
const render = (reactElementOrPrimitive, container) => {
if (["string", "number"].includes(typeof reactElementOrPrimitive)) {
const textNode = document.createTextNode(String(reactElementOrPrimitive));
container.appendChild(textNode);
return;
}
const realElement = document.createElement(reactElementOrPrimitive.tag);
if (reactElementOrPrimitive.props) {
Object.keys(reactElementOrPrimitive.props)
.filter((p) => p !== "children")
.forEach((p) => (realElement[p] = reactElementOrPrimitive.props[p]));
}
if (reactElementOrPrimitive.props.children) {
reactElementOrPrimitive.props.children.forEach((c) =>
render(c, realElement)
);
}
container.appendChild(realElement);
};
const reRender = () => {
stateCursor = 0;
document.querySelector("#app").firstChild.remove();
render(<App />, document.querySelector("#app"));
};
const stateStore = [];
let stateCursor = 0;
const useState = (initialState) => {
const fixedCursor = stateCursor;
stateStore[fixedCursor] = stateStore[fixedCursor] || initialState;
const setState = (newState) => {
stateStore[fixedCursor] = newState;
reRender();
};
stateCursor++;
return [stateStore[fixedCursor], setState];
};
const promiseCache = new Map();
const createResource = (promise, key) => {
if (promiseCache.has(key)) {
return promiseCache.get(key);
}
throw { promise, key };
};
const MyComponent = ({ key, initialValue, ...otherProps }) => {
const [value, setValue] = useState(initialValue);
return (
<div>
<label for={key}></label>
<input
id={key}
value={value}
onchange={(e) => setValue(e.target.value)}
{...otherProps}
/>
<p>{value}</p>
</div>
);
};
const MyAsyncComponent = () => {
const dogPhotoUrl = createResource(
fetch("https://dog.ceo/api/breeds/image/random")
.then((r) => r.json())
.then((data) => data.message),
"dogPhotoUrl"
);
return <img src={dogPhotoUrl}></img>;
};
const App = () => {
const [name, setName] = useState("Person");
const [count, setCount] = useState(0);
return (
<div className={"react"}>
<h1>Hello, {name}</h1>
<input
value={name}
onchange={(e) => setName(e.target.value)}
type="text"
placeholder="Write your name"
/>
<h2>Contagem é {count}</h2>
<button onclick={() => setCount(count + 1)}>+</button>
<button onclick={() => setCount(count - 1)}>-</button>
<MyComponent key="some-info" initialValue="Some Info" />
<MyComponent key="another-info" initialValue="Another Info" />
<MyAsyncComponent />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta dolorem
tenetur quos ipsam dolore explicabo consequatur vero architecto corrupti
id! Obcaecati cupiditate pariatur officia, doloremque sequi quia nulla
modi quo?
</p>
</div>
);
};
render(<App />, document.querySelector("#app"));