CodeNot attempted
shallowEqual (React.memo's comparison)
Difficulty: easy
Implement the comparison React.memo uses for props: same keys, and each value equal by Object.is, one level deep only. Then explain why an inline () => {} prop defeats it.
Show reference solution
export function shallowEqual(a: any, b: any): boolean { if (Object.is(a, b)) { return true; } if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) { return false; } const keysA = Object.keys(a); const keysB = Object.keys(b); if (keysA.length !== keysB.length) { return false; } // Only one level deep: compare each value with Object.is, don't look inside it. for (const key of keysA) { if (!Object.hasOwn(b, key) || !Object.is(a[key], b[key])) { return false; } } return true;}
Ctrl Enter