Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ScottAwesome opened this issue Jun 8, 2021 · 0 comments
Open

Comments

@ScottAwesome
Copy link
Contributor

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

@ScottAwesome ScottAwesome changed the title Propsoal: @wry/equality and handling react / preact specific code Proposal: @wry/equality and handling react / preact specific code Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant