Skip to content

Instantly share code, notes, and snippets.

@NarekChang
Created February 16, 2022 19:33
Show Gist options
  • Save NarekChang/2ae009664967802fcbb8fab53c17b227 to your computer and use it in GitHub Desktop.
Save NarekChang/2ae009664967802fcbb8fab53c17b227 to your computer and use it in GitHub Desktop.
let cursor = 0;
let myVal = [];
render(MyComp); // [201, 301, 401]
render(MyComp); // [202, 302, 402]
render(MyComp);
render(MyComp); // [204, 304, 404]
// cursor = 0, myVal = []
// cursor = 1, myVal = [200]
// cursor = 2, myVal = [200, 300]
function useState(initVal) {
const currCursor = cursor;
const setValue = (newVal) => {
myVal[currCursor] = newVal;
};
if (!myVal[currCursor]) setValue(initVal);
cursor += 1;
return [myVal[currCursor], setValue];
}
// cursor = 1, myVal = [200]
// cursor = 2, myVal = [200, 300]
// cursor = 3, myVal = [200, 300, 400]
function render(myFunc) {
cursor = 0;
const randNum = Math.random() > 0.5 ? 1 : 0;
myFunc(randNum);
}
function MyComp(randNum) {
console.log(">>>", randNum);
let [value4, setValue4] = [0, () => {}];
if (randNum) {
let [value4Hook, setValue4Hook] = useState(100);
value4 = value4Hook;
setValue4 = setValue4Hook;
}
let [value, setValue] = useState(200);
let [value2, setValue2] = useState(300);
let [value3, setValue3] = useState(400);
console.log(value4, value, value2, value3);
setValue4(value4 + 1);
setValue(value + 1);
setValue2(value2 + 1);
setValue3(value3 + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment