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

Using toJS inside observer component crashes in ES5 mode #2871

Closed
LinusCenterstrom opened this issue Apr 1, 2021 · 4 comments · Fixed by #2873 or #2842
Closed

Using toJS inside observer component crashes in ES5 mode #2871

LinusCenterstrom opened this issue Apr 1, 2021 · 4 comments · Fixed by #2873 or #2842
Labels
🐛 bug 🎁 mobx Issue or PR related to mobx package

Comments

@LinusCenterstrom
Copy link

Intended outcome:
Should be able to use toJS inside observer component

Actual outcome:
Mobx crashes with the message

[MobX] MobX is currently configured to be able to run in ES5 mode, but in ES5 MobX won't be able to iterate keys to detect added / removed properties. Use `keys` from 'mobx' instead.

How to reproduce the issue:
call

configure({
  useProxies: "ifavailable"
});

then use toJS inside an observer component

Sandbox:
https://codesandbox.io/s/immutable-leaf-lcoi8?file=/src/App.js

Versions
mobx: 6.1.8
mobx-react-lite: 3.2.0

If this is not fixable it should probably be documented.
There is an example that uses toJS here https://mobx.js.org/react-integration.html#dont-pass-observables-into-components-that-arent-observer and I assume that would crash in ES5 mode too

@LinusCenterstrom
Copy link
Author

LinusCenterstrom commented Apr 1, 2021

To fix this temporarly i copied over the relevant parts of the toJS code and changed the line throwing an error

It seems like changing

keys(source) // make sure keys are observed
const res = cache(__alreadySeen, source, {})
getPlainObjectKeys(source).forEach((key: any) => {
res[key] = toJSHelper(source[key], __alreadySeen)
})

to

 const res = cache(__alreadySeen, source, {}) 
 keys(source).forEach((key: any) => { 
     res[key] = toJSHelper(source[key], __alreadySeen) 
 }) 

fixes the problem

@iChenLei iChenLei added the 🎁 mobx Issue or PR related to mobx package label Apr 1, 2021
@mweststrate
Copy link
Member

mweststrate commented Apr 1, 2021 via email

@urugator
Copy link
Collaborator

urugator commented Apr 1, 2021

@mweststrate You sure? #2619 (comment)

I can fix this, I am just not sure which "keys" we should return, currently it returns own enumerable keys + own enumerable symbols, dunno if it still makes sense.

@mweststrate
Copy link
Member

mweststrate commented Apr 1, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug 🎁 mobx Issue or PR related to mobx package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants