-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact.ts
More file actions
42 lines (35 loc) · 906 Bytes
/
react.ts
File metadata and controls
42 lines (35 loc) · 906 Bytes
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
import { IGlobalState, ReactTag, JSX, IPropsWithChildren } from "./types";
import ReactDOM from "./react-dom";
const globalState: IGlobalState = {
states: [],
cursor: 0,
};
const React = {
createElement: (
tag: ReactTag,
props: IPropsWithChildren,
...children: JSX[]
): JSX => {
if (typeof tag === "function") {
return tag({ ...props, children });
}
return {
tag,
props: {
...props,
children,
},
};
},
useState<T>(initialValue: T): [state: T, setState: (newState: T) => void] {
const currentCursor = globalState.cursor;
const state = globalState.states[currentCursor] || initialValue;
const setState = (newValue: T) => {
globalState.states[currentCursor] = newValue;
ReactDOM.rerender(globalState);
};
globalState.cursor += 1;
return [state, setState];
},
};
export default React;