pmndrs / zustand

🐻 Bear necessities for state management in React

Home Page:https://zustand-demo.pmnd.rs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

shallow not support nested object

yinvoke opened this issue · comments

Bug

shallow not support nested object

Minimum reproduction

import { shallow } from 'zustand/shallow'

const a = { a : {  b : 1 } }
const b = { a : {  b : 1 } }
shallow(a,b)         // ==>false , except true

Details

  const keysA = Object.keys(objA)
  if (keysA.length !== Object.keys(objB).length) {
    return false
  }
  for (let i = 0; i < keysA.length; i++) {
    if (
      !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
      !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])    // if objA[keysA[i] is not a simple type ,will return false
    ) {
      return false
    }
  }

shallow means "shallow" equal, which compares only one level. If you need to compare nested object, it's deep equal.

See also: #2253