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

TypeError: Cannot perform 'get' on a proxy that has been revoked #10

Closed
relsunkaev opened this issue Mar 17, 2023 · 2 comments
Closed

Comments

@relsunkaev
Copy link

relsunkaev commented Mar 17, 2023

I have a use case where I access the base state in an onSuccess callback of a mutation. Something along the lines of

create((base) => {
  // Make changes to base
  mutation.mutate(..., { onSuccess: () => ctx.route.invalidate(base.id) })
})

However, it seems like base is a proxy object that gets revoked at some point between the call to mutation.mutate() and the call to onSuccess(). Currently I'm copying the base into a separate object before every mutation like this

create((base) => {
  // Make changes to base
  base = { ...base }
  mutation.mutate(..., { onSuccess: () => ctx.route.invalidate(base.id) })
})

but that is not ideal.

Is mutative actually passing a proxy object as base? If so, is this addressable on your end and/or are there any other workarounds I could use?

Note: this issue does not occur if running React in Strict Mode.

@unadlib
Copy link
Owner

unadlib commented Mar 18, 2023

@relsunkaev Mutative just drafts the baseState. I'm not sure how you're using it, but one thing is certain: the draft tree should not be used directly in any external function. You may encounter draft escaping, for example:

  let a: any;
  const state = create({ a: { b: 1 } }, (draft) => {
    draft.a.b = 2;
    a = draft.a;
  });
  // Don't use 'a', 'a' is a draft.
  expect(() => {
    JSON.stringify(a);
  }).toThrowError(`Cannot perform 'get' on a proxy that has been revoked`);
  // state.a is a object, not a draft

It would be helpful if you could provide a minimal example to help identify and troubleshoot the issue.

@relsunkaev
Copy link
Author

Yeah that was exactly what I was doing wrong. Closing the issue.

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

2 participants