reactjs - 为什么 React 组件在状态没有改变时只重新渲染一次?

标签 reactjs

import React, { useState } from "react";

export const App = () => {
  const [state, setState] = useState(0);
  console.log("Render App Render", stateA);

  return (
    <div className="App">
      <button onClick={() => setState(1)}>setState in App Component</button>
    </div>
  );
};
  1. 安装时 => “渲染应用渲染 0”
  2. 第一次点击按钮 => “RenderApp 渲染 1”
  3. 第二次点击按钮 => “渲染应用渲染 1”
  4. 第三次或更多点击:无日志!

可以帮我解释一下原因吗?

最佳答案

我能找到的最好的解释是 the following comment from a Facebook developer 。我链接的问题解释了同样的问题,渲染发生两次的原因是:

This is a known quirk due to the implementation details of concurrency in React. We don't cheaply know which of two versions is currently committed. When this ambiguity happens we have to over render once and after that we know that both versions are the same and it doesn't matter.

关于reactjs - 为什么 React 组件在状态没有改变时只重新渲染一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69668893/

相关文章:

javascript - 尝试使用 map.filter 写出一系列元素来过滤结果时出错

javascript - 如何使用对象点键和数组点图进行迭代以显示组件中的数据?

javascript - 有没有办法通过 props 将 FontAwesomeIcon 组件传递给 ReactHTMLTableToExcel 组件?

reactjs - React HOC 检查授权用户( protected 路由)

javascript - 客户聊天不适用于 React Messenger 客户聊天

javascript - 与基于类的方法相比,使用 Hooks 设置状态后重新渲染有什么区别?

javascript - React JS - 解析复杂的 Json 结构并填充到下拉列表中

reactjs - 使用 Jest 通过 props 模拟 React 组件

reactjs - 为什么 setState(this.state) 没有触发重新渲染?

javascript - React JS - 未捕获的类型错误 : Cannot read property 'bind' of undefined