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

Add reference equality checking to Pitfalls? #730

Closed
ianstormtaylor opened this issue Jan 7, 2021 · 3 comments · Fixed by #731
Closed

Add reference equality checking to Pitfalls? #730

ianstormtaylor opened this issue Jan 7, 2021 · 3 comments · Fixed by #731

Comments

@ianstormtaylor
Copy link
Contributor

🙋‍♂ Question

I ran into an issue because reference equality checking doesn't work with Immer proxies. After researching all the traps available to proxies the limitation makes total sense, but I think it's a pitfall if you aren't familiar with proxies. (I just assumed there was a trap for equality checks to make objects fully transparent.)

Here's an example of what's not possible:

import produce from 'immer'

const a = { value: 7 }
const b = { value: 4 }
const c = { value: 7 }
const values = [a, b, c]

const remove = produce((list, element) => {
  const index = list.indexOf(element);
  if (index > -1) list.splice(index, 1);
})

remove(values, a)

Since the proxies aren't referentially equal, you can't compare an existing element to the proxied elements to get its index. This makes using Immer in cases with elements without distinguishing properties impossible. (You need some sort of unique property like .id to identify the element in question for this to work.)

Is this correct?

I think it would be useful to add to the Pitfalls docs if so, and I'm happy to create a PR if you agree.

Link to repro

https://codesandbox.io/s/lucid-hooks-mst6l

@mweststrate
Copy link
Collaborator

mweststrate commented Jan 7, 2021 via email

@ianstormtaylor
Copy link
Contributor Author

@mweststrate ah cool, I didn't think of that! Thanks that's a great solution, I'll write up a PR that mentions that.

ianstormtaylor added a commit to ianstormtaylor/immer that referenced this issue Jan 8, 2021
Add a section about referential equality to pitfalls, with the simple 
example of using `indexOf` on a draft array to match an element.

Fixes immerjs#730
mweststrate pushed a commit that referenced this issue Jan 11, 2021
Add a section about referential equality to pitfalls, with the simple 
example of using `indexOf` on a draft array to match an element.

Fixes #730
@mweststrate
Copy link
Collaborator

🎉 This issue has been resolved in version 8.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants