React useState 源码极简版

作者:Administrator 发布时间: 2025-10-20 阅读量:7 评论数:0
let stateList = [];

let index = 0;

function useState(init) {
  let nowIndex = index++;
  if (stateList[nowIndex] === undefined) {
    stateList[nowIndex] = init;
  }
  return (
    [stateList[nowIndex]],
    (newState) => {
      stateList[nowIndex] = newState;
      render();
    }
  );
}

评论