why array extension method causes redux error: state mutation #4758
-
Hi guys, I wrote a couple of extension methods for arrays like this:
and then whether I use them or not (just by simply defining those extension methods) although I do not mutate anything, I have error for state mutation. The error is:
I'd like to know why is that and do I do anything wrong? can I even define extension methods for types that being used in redux states? the state type is an array of
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I have to be honest, i didn't know that people were still modifying built-in protoypes. As for Redux Toolkit itself, we require you to use plain objects and plain arrays in state. You fascinatingly found a way here to make arrays "not plain" anymore. I would recommend you do something like PS: Your implementation is also way slower than |
Beta Was this translation helpful? Give feedback.
I have to be honest, i didn't know that people were still modifying built-in protoypes.
That's widely considered a bad practice for about a decade at this point, I would really recommend that you don't do that - it will cause all kinds of weird behaviour since none of your dependencies expect that.
As for Redux Toolkit itself, we require you to use plain objects and plain arrays in state. You fascinatingly found a way here to make arrays "not plain" anymore.
I don't know if we can help you with that :/
I would recommend you do something like
arrayAny(arr, condition)
instead - or in this case, just useArray.prototype.some
which already exists.PS: Your implementation is also way slower than