--- pureComponent的shouldeComponentUpdate里,实际是对props/state进行了一个浅比较

这里实现一个hook版本的pureComponent


import { useEffect, useRef } from 'react'
import { isEqual } from 'lodash'

export default function useCompareEffect(effect: React.EffectCallback, dependencies?: Object) {
  function deepCompareDependencies(value: any) {
    const ref = useRef()

    if (!Object.is(value, ref.current)) { // 浅比较
      // if (!isEqual(value, ref.current)) { // 如果需要,可用深比较
      ref.current = value
    }

    return ref.current
  }

  useEffect(effect, deepCompareDependencies(dependencies))
}


相关文章:

  • 2021-04-26
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-07-18
  • 2021-09-01
  • 2022-02-07
猜你喜欢
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-03-03
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案