小编Ond*_*pek的帖子

useState() 做双重渲染

我在函数组件中使用 useState() 并且第一次渲染调用了两次。它是正确的还是错误的?如果正确,为什么要渲染两次?setCount 也渲染组件两次。

function Example() {
  const [count, setCount] = useState(0);
  console.log("render");

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

ReactDOM.render(<Example />, document.getElementById('uu5'));
Run Code Online (Sandbox Code Playgroud)

谢谢

render reactjs react-hooks

7
推荐指数
1
解决办法
3406
查看次数

标签 统计

react-hooks ×1

reactjs ×1

render ×1