首页 > 其他分享 >currentTuningFileInfoRef.current = useMemo(() => { return tuningFileOptionsMemo?.find(item =>

currentTuningFileInfoRef.current = useMemo(() => { return tuningFileOptionsMemo?.find(item =>

时间:2024-05-22 11:18:49浏览次数:12  
标签:tuningFileOptionsMemo currentTuningFileId useMemo item 使用 useEffect

在React中,useMemouseEffect 都是 Hooks,但它们各自有不同的用途和适用场景。上述代码片段使用了 useMemo 来 memoize(记忆化)一个计算结果,确保只要依赖项(tuningFileOptionsMemocurrentTuningFileId)不变,返回的引用也不会改变。这样做可以避免在每次渲染时都进行查找操作,从而提高性能。

如果考虑使用 useEffect 来实现类似功能,需要注意的是,useEffect 主要用于处理副作用,如执行异步操作、订阅或者手动更改DOM等,并不能直接替代 useMemo 的功能。
但是,你可以在 useEffect 中间接实现类似的效果,通过在每次依赖项变化时更新某个状态,但这通常不是最佳实践,因为它会导致组件不必要的重新渲染。

正确的做法是继续使用 useMemo 如上所示,因为它更适合于这个场景。
下面是一个尝试使用 useEffect 实现相似目的的示例,但请注意这不是推荐的做法,仅作为理解不同Hook用途的对比:

const [currentTuningFileInfo, setCurrentTuningFileInfo] = useState(null);

useEffect(() => {
  // 当依赖项变化时,查找并设置当前tuningFileInfo
  const fileInfo = tuningFileOptionsMemo?.find(item => item.id === currentTuningFileId);
  if (fileInfo) {
    setCurrentTuningFileInfo(fileInfo);
  }
}, [tuningFileOptionsMemo, currentTuningFileId]);

// 然后在组件中使用currentTuningFileInfo

在这个示例中,我们使用了一个状态 currentTuningFileInfouseEffect 来在依赖项变化时查找并设置值。
然而,这种方法会导致每次依赖项变化时组件都会重新渲染,即使查找的结果没有改变,这在性能上不如 useMemo 直接记忆化结果高效。
因此,对于此场景,原代码使用 useMemo 是更合适的选择。

标签:tuningFileOptionsMemo,currentTuningFileId,useMemo,item,使用,useEffect
From: https://www.cnblogs.com/longmo666/p/18205829

相关文章

  • Sitecore 设置 SelectItems 最大长度问题
    添加一个configpatch文件即可,然后放在App_Config下,内容如下:sitecore默认value是100。<?xmlversion="1.0"encoding="utf-8"?><configurationxmlns:patch="http://www.sitecore.net/xmlconfig/"xmlns:role="http://www.sitecore.n......
  • WPF ListBox acts image container, ItemTemplate,DataTemplate,
    <Windowx:Class="WpfApp100.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • [Javascript] Find Items from the end of the JavaScript Array using at, findLast
    Findingelementsstartingfromtheendofanarrayhasgottenaloteasierwiththeintroductionofthe at, findLast,and findLastIndex methods!With at younolongerneedtoremembertoaccesstheendofthearraylike array[array.length-1] trick.......
  • QStandardItemModel遍历查找搜索关键字
    (1)findItems查找内容筛选项,只能查找显示的文字中是否包含该文字,但是QList<QStandardItem*>findItems(constQString&text,Qt::MatchFlagsflags=Qt::MatchExactly,intcolumn=0)const;(2)mat......
  • QStandardItemModel 遍历勾选的项
    QStandardItemModel遍历勾选的项rowCount()不能传入 m_model->index(0,0)根节点,无法获取行数;不传,或者传入一个空QModelIndex对象,可以获取到第一级节点的数量;QMap<QString,QVariantMap>mapSelectVideo;introotRowCount=m_model->rowCount();for(inti=0;i<ro......
  • UITabBarController点击UITabBarItem 禁止跳转 iOS
    写在下面类里无效,切记classCJZFTabBarViewController:UITabBarController{} 需要写在appdelegate或者基类里,可以拦截tabbaritem是否被选中。选中了后,才会走  didSelect代理方法。 @interfaceBaseViewController()<UITabBarControllerDelegate>@end//判......
  • 前后端分离,提供蜘蛛爬行最简单方案,创建sitemap xml
    2024年5月13日11:36:01现在很多项目是vuereactangular开发的,但是百度爬虫对这样的项目支持不好,很多时候回去采用一些服务器端渲染(SSR)和静态站点生成(SSG),当然有些框架支持ssr和ssg效果不好,还有些想不不破坏项目自身的提前下的方案呢?很多年前接手一个angularjs的项目,但是......
  • How to Learn Item Representation for Cold-Start Multimedia Recommendation
    目录概符号说明MotivationMulti-TaskPairwiseRanking(MTPR)代码DuX.,WangX.,HeX.,LiZ.,TangJ.andChuaT.Howtolearnitemrepresentationforcold-startmultimediarecommendation?MM,2020.概作者以往的多媒体推荐对于colditems在训练阶段没有足够的......
  • TypeError 'tuple' object does not support item assignment
    左手编程,右手年华。大家好,我是一点,关注我,带你走入编程的世界。公众号:一点sir,关注领取编程资料TypeError:'tuple'objectdoesnotsupportitemassignment是一个在Python编程语言中常见的错误,意味着你试图修改一个不可变的元组(tuple)对象中的元素。在Python中,元组是一种不......
  • WPF CollectionViewSource GroupDescriptions GroupStyle ItemsPanelTemplate
    <Windowx:Class="WpfApp83.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......