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 new Array(n) constructor in snapshot doesn't work #1912

Open
3 tasks done
davetapley opened this issue May 18, 2022 · 2 comments
Open
3 tasks done

Using new Array(n) constructor in snapshot doesn't work #1912

davetapley opened this issue May 18, 2022 · 2 comments
Labels

Comments

@davetapley
Copy link
Contributor

Bug report

  • I've checked documentation and searched for existing issues
  • I've made sure my project is based on the latest MST version
  • Fork this code sandbox or another minimal reproduction.

Sandbox link or minimal reproduction code
https://codesandbox.io/s/mobx-state-tree-using-new-array-n-constructor-in-snapshot-doesnt-work-ehd5ib

Describe the expected behavior
If I use Array.new(3) for a types.array(types.maybe(types.number)) it should be [undefined,undefined,undefined].

Describe the observed behavior
It is put in the store as an empty array [].

Even crazier
If you a = Array.new(3); a.fill(undefined), then it correctly inserts [undefined,undefined,undefined] 🤯

@diegochavez
Copy link
Collaborator

Hi Dave,

New Array generates an array with a length of 3, not including values. that's way is undefined
the generated array looks like this.
[key]:value
0:undefined
1:undefined
2:undefined

For new array from a number you should do,
const stuff = [...new Array(3).keys()];
const stuff = Array.from(new Array(3).keys());

@coolsoftwaretyler
Copy link
Collaborator

Hey @davetapley - did the response from @diegochavez solve your problem? If so, let me know and we can close this out.

Since it's been over a year since there was activity here, I'll probably close this out if I don't hear from any of y'all in the next two weeks or so.

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

No branches or pull requests

3 participants