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

Calling an object getter on an immerable draft occurs an Error. #439

Closed
1 of 2 tasks
luncheon opened this issue Oct 7, 2019 · 3 comments
Closed
1 of 2 tasks

Calling an object getter on an immerable draft occurs an Error. #439

luncheon opened this issue Oct 7, 2019 · 3 comments

Comments

@luncheon
Copy link

luncheon commented Oct 7, 2019

🐛 Bug Report

Calling an object getter on an immerable draft and mutate another property occurs an Error.

TypeError: Cannot set property bar of # which has only a getter

This error is not occured when setUseProxies(false).

To Reproduce

import { produce, immerable } from "immer";

class Obj {
  constructor() {
    this.foo = 0;
    this._bar = { baz: 1 };
  }

  get bar() {
    return this._bar;
  }

  syncFoo() {
    return produce(this, state => {
      state.foo = state.bar.baz;
    });
  }
}

Obj.prototype[immerable] = true;

const obj = new Obj();
const obj2 = obj.syncFoo();

Expected behavior

The error is not occurred.

Link to repro (highly encouraged)

CodeSandbox

Environment

  • Immer version: 4.0.1
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@dalcib
Copy link
Contributor

dalcib commented Oct 7, 2019

It can be fixe by the PR #438.

image

@luncheon
Copy link
Author

luncheon commented Oct 7, 2019

All right. Thank you. I'm looking forward to merging the PR!

mweststrate added a commit that referenced this issue Jun 10, 2020
* Introduced `current`, which takes a snapshot of the current state of a draft and finalizes it (but without freezing). Current is a great utility to print the current state during debugging (no Proxies in the way), and the output of current can also be safely leaked outside the producer. Implements #441, #591
* [BREAKING CHANGE] getters and setters are now handled consistently: own getters and setters will always by copied into fields (like Object.assign does), inherited getters and setters will be left as-is. This should allow using Immer directly on objects that trap their fields, like down in Vue or MobX. Fixes #584, #439, #593, #558
* [BREAKING CHANGE] produce no longer accepts  non-draftable objects as first argument
* [BREAKING CHANGE] original can only be called on drafts and will throw otherwise (fixes #605)
* [BREAKING CHANGE] non-enumerable and symbolic fields will never be frozen
* [BREAKING CHANGE] the patches for arrays are now computed differently to fix some scenarios in which they were incorrect. In some cases they will be more optimal now, in other cases less. Especially splicing / unshifting items into an existing array might result in a lot of patches. Fixes #468
* Improved documentation in several areas, there is now a page for typical update patterns and a separate page on how to work with classes. And additional performance tips have been included. Fixes #457, #115, #462
* Fixed #462: All branches of the produced state should be frozen
* Fixed #588: Inconsistent behavior with nested produce
* Fixed #577: Immer might not work with polyfilled symbols
* Fixed #514, #609: Explicitly calling `useProxies(false)` shouldn’t check for the presence of Proxy.
@mweststrate
Copy link
Collaborator

Immer 7 has been released and handles getters and setters differently from the previous version. Details can be found here: https://immerjs.github.io/immer/docs/complex-objects#semantics-in-detail

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.

3 participants