我看到一个帖子是关于如何防止重新渲染,以及添加太多备忘录的.

发帖:https://d2.naver.com/helloworld/9223303 (它是用韩语写的,我不确定它是否有英文版本)

在这个过程中,它说

消除不必要的记忆的方法之一是使用包装组件可以避免重新呈现子组件 代码如下:

function ChildMemo () {  
  const [count, setCount] = React.useState(0);

  const doubleCount = count * count;
  console.log('doubleCount', count * count);

  return (
    <>
      <h2>count: {count}</h2>
      <h2>count * count: {doubleCount}</h2>
      <button onClick={() => setCount((prev) => prev + 1)}>Click</button>
    </>
  )
}

function ParentMemo({ children }) {  
  const [time, setTime] = React.useState(new Date());

  React.useEffect(() => {
    const intervalId = setInterval(() => {
      setTime(new Date());
    }, 1000);

    return () => {
      clearInterval(intervalId);
    };
  }, []);

  return (
    <>
      <h1>time: {format(time, 'hh:mm:ss')}</h1>
      {children}
    </>);
}

export default function Memo() {  
  return <ParentMemo>
    <ChildMemo/>
  </ParentMemo>
};

(代码从this posting开始)

实际上,当我运行上面的代码时,我可以看到子组件没有重新呈现.

作为参考, 下面的代码我稍加改动就会发生子组件重新呈现,导致父组件重新呈现与更新状态导致子组件重新呈现.

// this code doesn't have wrapping component, and there's ChildMemo re-rendering
// ChildMemo re-render at every single seconds(ParentMemo component re-rendering)
import "./styles.css";
import * as React from "react";

export default function App() {
  return (
    <div className="App">
      <ParentMemo />
    </div>
  );
}

function ChildMemo() {
  const [count, setCount] = React.useState(0);

  const doubleCount = count * count;
  console.log("doubleCount", count * count);

  return (
    <>
      <h2>count: {count}</h2>
      <h2>count * count: {doubleCount}</h2>
      <button onClick={() => setCount((prev) => prev + 1)}>Click</button>
    </>
  );
}

function ParentMemo({ children }) {
  const [time, setTime] = React.useState(new Date());

  React.useEffect(() => {
    const intervalId = setInterval(() => {
      setTime(new Date());
    }, 1000);

    return () => {
      clearInterval(intervalId);
    };
  }, []);

  return (
    <>
      <h1>time: {time.getTime().toString()}</h1>
      <ChildMemo />
    </>
  );
}

有谁知道为什么像上面这样的包装组件可以防止任何机会重新渲染?

推荐答案

当然!包装组件可以防止重新呈现,因为它充当父组件和子组件之间的屏障.当父组件重新呈现时,它可能不一定使子组件重新呈现if the child component is not directly dependent on the state or props of the parent.

在提供的示例中,当ChildMemo组件包装在ParentMemo组件中时,它将ChildMemo与父对象的状态(时间)的更改隔离开来.由于ChildMemo不直接依赖于时间状态,因此当ParentMemo由于时间变化而重新渲染时,它不会不必要地重新渲染.这种隔离可防止不必要的ChildMemo重新渲染.

Reactjs相关问答推荐

阻止在ShadCN和React中的FormLabel中添加 colored颜色

呈现组件元素(MAP)中的问题

获取未捕获的错误:Gatsby生产版本上的最小化react 错误#418

在新屏幕上显示照片时出现问题-react

Next.js Next-Auth登录函数重定向到http://localhost:3000/api/auth/error

在transformResponse中使用来自其他查询的缓存

GitHub页面配置

为什么react日历时间轴项Renderprops 不能与useState挂钩一起使用?

单击按钮不会切换组件(当按钮和组件位于两个单独的文件中时)

React-chartjs-2:红色和绿色之间的差距

useEffect 和 ReactStrictMode

使用获取的数据更新状态,但在try console.log 时它给出未定义

显示我是否加入聊天应用程序时出现问题

antd 找不到 comments

在 Spring Cloud Gateway 中运行的 React SPA 页面刷新出现白标错误

将 Material UI 菜单渲染为

在 Redux 中更新布尔状态时出错

bootstrap 导航栏切换器在 reactjs 中不起作用

错误未捕获的错误:[App] 不是 组件. 的所有子组件必须是

添加禁用的默认选项以 Select