immerjs / immer

Create the next immutable state by mutating the current one

Home Page:https://immerjs.github.io/immer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

import and call `enableMapSet()` error happens even after enableMapSet() is called.

github0013 opened this issue · comments

To Reproduce

Steps to reproduce the behavior:

https://codesandbox.io/p/sandbox/thirsty-dan-4e3o9r

  1. Open and wait, and you will see the error.
  2. Notice enableMapSet() in the code, but the error says

The plugin for 'MapSet' has not been loaded into Immer. To enable the plugin, import and call enableMapSet() when initializing your application.

Change the version to 9.0.12, then restart. Now the error is gone.

Observed behavior

A description of what behavior you observed and why you consider it faulty.

  • The version on the codesandbox is immer@9.0.19, but this error doesn't happen if it was immer@9.0.12.
  • This happens under nextjs projects. If the same code is run under a regular react app, things work without an error.

Expected behavior

A clear and concise description of what you expected to happen instead.

  • immer@9.0.13 - 9.0.19 shouldn't not show the error.

Environment

We only accept bug reports against the latest Immer version.

  • Immer version:
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)

Typically one of two things is happening here:

  1. enableMapSet is called too late (it's generally hard to control that in JS, as module loading order is not always what you'd expect it to be
  2. there are two different versions of Immer in your dependency tree, and enableMapSet is imported from the different one as that is running the produce later. yarn why can be a grat help for that (there is probably an NPM equivalent)
  1. so .. if I change the immer version, the error doesn't happen. Is it still an external problem?

Change the version to 9.0.12, then restart. Now the error is gone.

  1. I am reproducing the same error on codesandbox, so I don't think there are 2 different versions of immer.

I don't know how to test it in your sandbox, but it seems you have both xstate/immer and immer as dependencies, so enableMapSet is probably called on the wrong one, and I guess you don't need to install immer manually, or make sure to dedupe it.

I guess you don't need to install immer manually,

So I removed immer, then I get this.

./node_modules/@xstate/immer/lib/index.js:5:0
Module not found: Can't resolve 'immer'

And I don't see enableMapSet is used in @xstate/immer. Again, this error doesn't happen under the same code if I downgrade immer version to 9.0.12.

I just checked the @xstate/immer sources, they correctly set up immer as peer dependency, so it indeed needs to be included, and enableMapSet should hence affect it correctly.

So I think this issue is specifically caused by Next.js. I don't know how Next.js works, what is bundled on server vs client, or whether it brings an own internal 9.0.12 Immer version somewhere. So I think you can best raise this one with the Next.js community.

Edit: or you can ask @xstate/immer folks to enable MapSet by default, that'd fix the issue as well I suspect