-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhooks.js
More file actions
103 lines (81 loc) · 3.54 KB
/
hooks.js
File metadata and controls
103 lines (81 loc) · 3.54 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
let React = (function() {
let global = {}; // define a global variable where we store information about the component
let index = 0; // index to keep track of the component's state
function render(Component) {
global.Component = Component;
const instance = Component(); // get the instance of the component
index = 0;
instance.render(); // call the component's render function
global.instance = instance; // store the component's instance for any future calls of the component's functions
return global; // return the global variable
}
function useState(initialState) {
if (!global) {
throw new Error("Need a global");
}
if (!global.hooks) {
global.hooks = []; // this array holds the state of the component
}
const hooks = global.hooks;
const currentState = global.hooks[index] || initialState;
hooks[index] = currentState; // memoize the state for future access
firstrender = true;
const setState = (function() {
let currentIndex = index; // copy the index so each useState call will have it's own "closed" value over index (currentIndex)
return function(value) {
global.hooks[currentIndex] = value;
render(global.Component); //re-render the component after state change
};
})();
index = index + 1;
return [currentState, setState];
}
function useEffect(cb, deps) {
const hooks = global.hooks;
// getting older dependencies from the hooks array since
// we are storing dependencies as a sub-array inside the hooks array
let oldDeps = hooks[index];
// if no dependencies are provided,
// the callback function will be called at each re-render
let hasChanged = true;
if (oldDeps) {
// checking if the old dependencies are different from older dependencies
hasChanged = deps.some((d, index) => !Object.is(d, oldDeps[index]));
}
if (hasChanged) cb(); // if dependencies has changed call the callback function.
hooks[index] = deps; //store dependencies inside the hooks array as a sub-array
index++; // increment index for any other useEffect calls
}
return { render, useState, useEffect };
})();
function Component() {
// Component is called at each re-render. index is reset to 0.
const [count, setCount] = React.useState(0);
// hooks: [0], currentIndex: 0, Incremented Index: 1
const [word, setWord] = React.useState("");
// hooks: [0, ''], currentIndex: 1, Incremented Index: 2
const countSetter = () => {
setCount(count + 1);
};
const wordSetter = word => {
setWord(word);
};
function render() {
console.log(`Count is: ${count}, Word is: ${word}`);
}
React.useEffect(() => {
console.log("hookssss!!!!");
}, [count, word]);
// hooks: [0, '', [0, '']], currentIndex: 2, Incremented Index: 3
React.useEffect(() => {
console.log("hooks2!!!!!");
}, []);
// hooks: [0, '', [0, ''], [] ], currentIndex: 3, Incremented Index: 4
return { render, countSetter, wordSetter };
}
const global = React.render(Component); // hooks: [ 0, '', [ 0, '' ], [] ]
global.instance.countSetter(); // hooks: [ 1, '', [ 1, '' ], [] ]
global.instance.countSetter(); // hooks: hooks: [ 2, '', [ 2, '' ], [] ]
global.instance.countSetter(); // hooks: [ 3, '', [ 3, '' ], [] ]
global.instance.wordSetter("yooo"); // hooks: [ 3, 'yooo', [ 3, 'yooo' ], [] ]
global.instance.wordSetter("ssup"); // hooks: [ 3, 'yooo', [ 3, 'yooo' ], [] ]