当前位置: 首页 > news >正文

推荐好的设计网站404网站页面进入

推荐好的设计网站,404网站页面进入,网站建设公司提成,自己做的网站访问不目录 背景优化方案方法一:提取到父组件,分离 Timer 和 OtherSlowComponent方法二:使用 useMemo 缓存慢组件(更适用于传了 props 的场景)方法三:memo方法四:props.children为什么 props.children…

目录

  • 背景
  • 优化方案
    • 方法一:提取到父组件,分离 Timer 和 OtherSlowComponent
    • 方法二:使用 useMemo 缓存慢组件(更适用于传了 props 的场景)
    • 方法三:memo
    • 方法四:props.children
      • 为什么 props.children 能帮助优化
      • 示例:使用 props.children 优化重渲染
      • 渲染优化分析
    • 总结

背景

有如下组件:

const Timer = () => {const [time, setTime] = useState(0); // 每秒更新return (<><h1>当前时间:{dayjs(time).format("YYYY-MM-DD HH:mm:ss")}</h1><OtherSlowComponent /> // 每次 setTime 都会触发整个组件重新渲染,包括这里</>);
};const Container = () => <Timer />;

每秒更新一次 time,导致整个 Timer 组件重新渲染,连带 ·OtherSlowComponent· 也每秒被重新渲染一次,哪怕它跟时间无关!

又或者:

import { memo } from 'react';export const App = () => {const { currentUserInfo, users } = useGetInfoData();return (<div className="App"><CurrentUserInfo data={currentUserInfo} /><UserInfoList users={users} /></div>);
};

假如 currentUserInfo 更新,也会导致 UserInfoList 组件的重新渲染。

优化方案

方法一:提取到父组件,分离 Timer 和 OtherSlowComponent

const Container = () => {return (<><Timer /><OtherSlowComponent /></>);
};

如果 OtherSlowComponentTimer 没有任何依赖关系,直接将它们并列在父组件中是最简单高效的做法,彻底避免无意义的重渲。

方法二:使用 useMemo 缓存慢组件(更适用于传了 props 的场景)

const memoizedSlowComp = useMemo(() => <OtherSlowComponent />, []);

但注意:useMemo 仅在初次渲染创建,依赖项变化时才会重新计算。

方法三:memo

export const App = () => {const { currentUserInfo, users } = useGetInfoData();return (<div className="App"><CurrentUserInfo data={currentUserInfo} /><UserInfoList users={users} /></div>);
};const UserInfoList = memo(({ users }) => {// ...
});

方法四:props.children

为什么 props.children 能帮助优化

如果你把慢组件 OtherSlowComponent 作为 children 传进去,并且 Timer 本身不关心它、也不依赖它的任何状态变化,那就可以很好地“托管”渲染时机 —— 只要父组件不变,它不会重渲!

示例:使用 props.children 优化重渲染

const Container = () => {return (<Timer><OtherSlowComponent /></Timer>);
};const Timer = ({ children }: { children: React.ReactNode }) => {const [time, setTime] = useState(new Date());useEffect(() => {const intervalId = setInterval(() => setTime(new Date()), 1000);return () => clearInterval(intervalId);}, []);return (<><h1>当前时间: {dayjs(time).format("YYYY-MM-DD HH:mm:ss")}</h1>{children}</>);
};const OtherSlowComponent = React.memo(() => {console.log('💡 OtherSlowComponent 渲染了');return <div>这是一个渲染慢的组件</div>;
});

渲染优化分析

组件是否每秒重渲染?原因
Timer每秒更新 time
OtherSlowComponent被当作 children 静态传入,不受 time 影响,且 React.memo 避免重渲染

更多关于 props.children 的内容可以阅读 WHAT - React 组件的 props.children 属性

总结

方法是否推荐说明
使用 props.children✅✅✅非常推荐,结构清晰、性能佳
React.memo 搭配 children更加保险,确保不被不必要的重渲染触发
放到父组件里如果结构允许,这是最清晰的做法
http://www.dtcms.com/a/489500.html

相关文章:

  • 网站开发 用户角色贵州建设监督管理局网站
  • 焦作网站开发公司电话平凉哪家做企业网站
  • 网站为什么没有排名了水溶花边外发加工网
  • wordpress网站下方网站 中国最早做网站的
  • 网站建设与网页制作盒子模型做商城网站需要在北京注册公司吗
  • 中国住房和城乡建设网网站网站建设实训心得体会300字
  • 企业网站页脚信息wordpress链接样式表
  • 我的家乡网页制作素材二十个优化
  • 免费网站建设的wordpress主题少儿教育类
  • 卖磁铁的网站怎么做莱芜网站建设设计外包
  • 备案期间网站能用吗全球咨询公司最新排名
  • wordpress 下载站模板汽车配件加工网
  • 自动优化网站建设咨询wordpress配置伪静态
  • 网站的栏目设计怎么做企业网站的建设目的有哪些
  • 手工艺品出口网站建设策划书做一个英文的公司网站多少钱
  • 网站开发之后如何上传源码外贸网站如何做外链
  • 企业网站建设三网合一网站建设公司如何签单
  • 国外 网站页面wordpress html 模板
  • 怎做网站转app弹幕怎么做视频网站
  • php网站源代码苏州优化件
  • 网站服务器干啥2345浏览器主页网址
  • 什么网站做美食最好最专业网站建设的软件是哪个
  • d网站建设的目的你好南京网站
  • 做打井宣传广告找什么网站软件官网模板
  • 了解网站建设的基本流程做此广告的网站
  • 如何做分享赚钱的网站如何做网站 写代码
  • 用什么做网站毕业设计做网站想法
  • 好口碑的网站制作安装价格西安网络建设公司
  • sqlite做网站深圳画册设计龙华
  • 做网站好一点的公司wordpress数据承载能力