Skip to content

Proposal: @wry/equality and handling react / preact specific code #161

Description

@ScottAwesome

I have a relatively simple proposal, which is handling of equality comparisions for react / preact elements.

I think, more or less, it would look like the code below, (though I would need to do some detection mechanism to figure out if its a react / preact object or not)

if (hasElementType && a instanceof Element) {
  return false;
}

// this would be the react object
const keys = Object.keys(a);
const length = keys.length;
// custom handling for React/Preact
for (let i = length; i-- !== 0; ) {
  if ((keys[i] === '_owner' || keys[i] === '__v' || keys[i] === '__o') && a.$$typeof) {
    // React-specific: avoid traversing React elements' _owner
    // Preact-specific: avoid traversing Preact elements' __v and __o
    //    __v = $_original / $_vnode
    //    __o = $_owner
    // These properties contain circular references and are not needed when
    // comparing the actual elements (and not their owners)
    // .$$typeof and ._store on just reasonable markers of elements

    continue;
  }

  // all other properties should be traversed as usual
  if (!equal(a[keys[i]], b[keys[i]])) {
    return false;
  }
}

This would make it safe to use for equality with APIs like React.memo and prop comparisions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions