--- 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))
}