当我们使用 useSelector 挂钩时,每当我更改状态时,我使用 useSelector 挂钩的所有组件都会重新渲染
如何避免这种情况以及是否有其他方法可以防止组件重新渲染
useSelector 采用第二个参数,该函数检查先前状态和当前状态是否相等以确定何时更新。
import { shallowEqual, useSelector } from 'react-redux'
// Pass it as the second argument directly
const selectedData = useSelector(selectorReturningObject, shallowEqual)
// or pass it as the `equalityFn` field in the options argument
const selectedData = useSelector(selectorReturningObject, {
equalityFn: shallowEqual,
})