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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array out-of-range assignments are not picked up properly in IE 11 #638

Closed
toli444 opened this issue Jul 7, 2020 · 6 comments
Closed

Array out-of-range assignments are not picked up properly in IE 11 #638

toli444 opened this issue Jul 7, 2020 · 6 comments

Comments

@toli444
Copy link

toli444 commented Jul 7, 2020

馃悰 Bug Report

If my state is an object and I set a property to it of array with not existed index:

import { set } from 'lodash';

const state = {
    field: []
} 

produce(state, draft => {
     set(draft, 'field[2]', '2')

     return draft
}

I get this as a result:

{
     field: [undefined, undefined, '2']
}

and if then I set some other array index that goes before the one I used previously:

set(draft, 'field[1]', '1')

I still get:

{
     field: [undefined, undefined, '2']
}

Whereas I would expect to get:

{
     field: [undefined, '1' , '2']
}

Additional context

It works fine in Chrome, Firefox, also when you first set index 1 then 2 (in my example I have the bug because I first set 2 then 1) and also it works as expected when you explicitly define the array with undefined values or something (like [undefined, undefined, undefined])

Environment

IE11
Immer version 6+

@toli444 toli444 changed the title State is not updated if Array was not initialized first in IE11 State is not updated if an array was not initialized first in IE11 Jul 7, 2020
@mweststrate
Copy link
Collaborator

Pretty sure the problem is solved if you don't use lodash.set. There is really no reason to use lodash.set if you are using immer. I'll expect draft.field[2] = 2 will already do the trick. If it doesn't you might need to set a length explicitly first. But in any case, bug reports without reproduction are inactionable, so please attach a reproduction or close the issue. IE 11 behavior can often be simulate by explicitly setting useProxies(false)

@toli444
Copy link
Author

toli444 commented Jul 7, 2020

@mweststrate here is codesandbox with the issue (w/o lodash):

https://codesandbox.io/s/laughing-lovelace-rfxhc?file=/src/index.js

  • First click on the first button, then on the second - no issue.
  • Reload the page
  • First click on the second button then on the first - issue occures.

@toli444 toli444 changed the title State is not updated if an array was not initialized first in IE11 (Details added) State is not updated if an array was not initialized first in IE11 Jul 13, 2020
@toli444
Copy link
Author

toli444 commented Jul 15, 2020

Any updates on this issue?

@mweststrate
Copy link
Collaborator

Looks like a bug indeed. But note that out-of-range assignments are an anti pattern though (which is probably why nobody did run into this before). I recommend to use push to add items, or if your collection is intended to be sparse, to use maps.

@mweststrate mweststrate changed the title (Details added) State is not updated if an array was not initialized first in IE11 Array out-of-range assignments are not picked up properly in IE 11 Jul 22, 2020
@mweststrate
Copy link
Collaborator

Thanks for the detailed report!

@aleclarson
Copy link
Member

馃帀 This issue has been resolved in version 7.0.7 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

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

No branches or pull requests

3 participants