mobx-state-tree
mobx-state-tree copied to clipboard
Using new Array(n) constructor in snapshot doesn't work
Bug report
- [x] I've checked documentation and searched for existing issues
- [x] I've made sure my project is based on the latest MST version
- [x] 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] 🤯
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());
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.