iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Reactmemo减少重复渲染详解
  • 534
分享到

Reactmemo减少重复渲染详解

Reactmemo减少重复渲染Reactmemo 2022-11-13 18:11:12 534人浏览 泡泡鱼
摘要

目录1. 概述2. 使用1. 概述 此方法是一个 React 顶层 api 方法,给函数组件来减少重复渲染,类似于 PureComponent 和 shouldComponentUp

1. 概述

此方法是一个 React 顶层 api 方法,给函数组件来减少重复渲染,类似于 PureComponent 和 shouldComponentUpdate 方法的集合体。

React.memo顶层Api方法,它可以用来减少子组件的重复渲染次数,从而提升组件渲染性能。

React.memo它是一个只能在函数组件中使用的顶层Api方法。

当父组件发生改变时,默认情况下它的子孙组件也会重新渲染,当某些子组件不需要更新时,也会被强制更新,为了避免这种情况,我们可以使用 React.memo。

2. 使用

在不使用 React.memo 方法的情况下,子组件即使和父组件没有任何关联,只要父组件更新,子组件也会跟着更新:

import React, { useState, memo } from 'react'
const Child = () => {
  console.log('child')
  return (
    <div>
      <h3>child组件</h3>
    </div>
  )
}
const App = () => {
  let [count, setCount] = useState(100)
  let [name, setName] = useState('张三')
  return (
    <div>
      <h3>App -- {count}</h3>
      <button onClick={() => {
        setCount(v => v + 1)
      }}>
        ++++
      </button>
      <Child />
    </div>
  )
}
export default App

上面的方案对性能的消耗很大,于是我们使用 React.memo 方法来解决这个问题,我们可以这样写:

import React, { useState, memo } from 'react'
const Child = memo(() => {
  console.log('child')
  return (
    <div>
      <h3>child组件</h3>
    </div>
  )
})
const App = () => {
  let [count, setCount] = useState(100)
  let [name, setName] = useState('张三')
  return (
    <div>
      <h3>App -- {count}</h3>
      <button onClick={() => {
        setCount(v => v + 1)
      }}>
        ++++
      </button>
      <Child />
    </div>
  )
}
export default App

我们可以用一个更直观的例子来展示 React.memo 的作用:

import React, { useState, memo } from 'react'
// React.memo顶层Api方法,它可以用来减少子组件的重复渲染次数,从而提升组件渲染性能
// React.memo它是一个只能在函数组件中使用的顶层Api方法
const Child = memo(({count}) => {
  console.log('child')
  return (
    <div>
      {}
      <h3>child组件 -- {count}</h3>
    </div>
  )
})
const App = () => {
  let [count, setCount] = useState(100)
  let [name, setName] = useState('张三')
  return (
    <div>
      <h3>App -- {count}</h3>
      <input type="text" value={name} onChange={e => setName(e.target.value)} />
      <button onClick={() => {
        setCount(v => v + 1)
      }}>
        ++++
      </button>
      <Child count={count} />
    </div>
  )
}
export default App

React.memo 中还可以写回调函数:

import React, { useState, memo } from 'react'
// shouldComponentUpdate它必须要有一个返回值,true则表示继续渲染,false停止渲染
// React.memo参数2返回值如果为true则表示停止渲染,false继续渲染
const Child = memo(
  ({ count }) => {
    console.log('child')
    return (
      <div>
        <h3>child组件 -- {count}</h3>
      </div>
    )
  },
  // prevProps 旧的props数据   object
  // nextProps 新的props数组   object
  // 可以比较两者的不同,来决定是否重新渲染
  // 参数2写的回调函数,一般情况下都在props传过来的数据为引用类型,才需要手动来判断,如果是基本类型则不需要写参数2,来手动判断。
  (prevProps, nextProps) => {
    // true/false
    // return false
    // console.log(prevProps, nextProps)
    return prevProps.count === nextProps.count
  }
)
const App = () => {
  let [count, setCount] = useState(100)
  let [name, setName] = useState('张三')
  return (
    <div>
      <h3>App -- {count}</h3>
      <input type="text" value={name} onChange={e => setName(e.target.value)} />
      <button onClick={() => {
        setCount(v => v + 1)
      }}>
        ++++
      </button>
      <Child count={count} />
    </div>
  )
}
export default App

上文说到 React.memo 中参数2写的回调函数,一般情况下都在 props 传过来的数据为引用类型,才需要手动来判断,如果是基本类型则不需要写参数2,来手动判断。所以我们下面来看一个传值为引用类型的例子:

import React, { useState, memo } from 'react'
import _ from 'lodash'
// shouldComponentUpdate它必须要有一个返回值,true则表示继续渲染,false停止渲染
// React.memo参数2返回值如果为true则表示停止渲染,false继续渲染
const Child = memo(
  ({ count }) => {
    console.log('child')
    return (
      <div>
        <h3>child组件 -- {count.n}</h3>
      </div>
    )
  },
  // 使用lodash库来完成对象的值的比较,从而用来完成减少组件的无用的重复渲染
  (prevProps, nextProps) => _.isEqual(prevProps, nextProps)
)
const App = () => {
  // let [count, setCount] = useState(100)
  let [count, setCount] = useState({ n: 100 })
  let [name, setName] = useState('张三')
  return (
    <div>
      {}
      <h3>App -- {count.n}</h3>
      <input type="text" value={name} onChange={e => setName(e.target.value)} />
      <button
        onClick={() => {
          setCount({ n: Date.now() })
        }}
      >
        ++++
      </button>
      <Child count={count} />
    </div>
  )
}
export default App

注意:不使用参数2的时候,假设对象中属性的值没变,子组件在这种情况下也一定会重新渲染,这是因为对象的引用地址变了。

到此这篇关于React memo减少重复渲染详解的文章就介绍到这了,更多相关React memo内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Reactmemo减少重复渲染详解

本文链接: https://www.lsjlt.com/news/169589.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作