我正在研究React-Redux-v.7.1提供的 Hooks API 如何工作,并在平等比较和更新(https://react-redux.js.org/api/hooks#equality-comparisons-and-更新):
"从 v7.1.0-alpha.5 开始,默认比较是严格的===引用比较。这与connect()不同,后者使用对mapState调用结果的浅层相等性检查来确定是否需要重新渲染. 这对您应该如何使用useSelector()有几个影响。 ”
我想知道为什么严格相等比connect()使用的浅相等更好?然后我查看了他们的平等比较:
useSelector()的默认严格相等检查只是检查a===b
const refEquality = (a, b) => a === b
Run Code Online (Sandbox Code Playgroud)
并且react-redux/src/connect/connect.js 中的相等性检查使用Object.is()和其他检查,这与react 中的相同。
// The polyfill of Object.is()
function is(x, y) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y …Run Code Online (Sandbox Code Playgroud)