🧑‍💻 Custom useState


let counter = 0;
const hooks = [];
function useState(init) {
  if (hooks[counter]) return hooks[counter++];
  const hook = [init, (val) => hook[0] = val];
  return hooks[counter++] = hook;
}

function callComponent(Comp) {
  counter = 0;
  Comp();
}

function Comp() {
  const [state1, setState1] = useState(0);
  const [state2, setState2] = useState(1);
  const [state3, setState3] = useState(2);

  setState1(5);
  if (state1 == 5) {
    setState1(0);
    setState2(10);
  }

  setState3(state3 + 1);
  setState3(state3 + 1);
  setState3(state3 + 1);

  console.log(state1, state2, state3);
}

callComponent(Comp);
callComponent(Comp);
callComponent(Comp);
Output:
0 1 2
5 1 3
0 10 4