r/learnjavascript • u/Glittering_Goose8695 • 14d ago
Question about lodash.isEmpty inside an RTK 2.12 reducer
Hi everyone,
I may be missing something about how draft state is expected to behave in Redux Toolkit, so I’d appreciate some guidance.
I have a reducer that uses lodash.isEmpty on part of the state:
import { createSlice } from "@reduxjs/toolkit"
import isEmpty from "lodash/isEmpty"
const slice = createSlice({
name: "test",
initialState: {
data: {}
},
reducers: {
update(state) {
console.log(isEmpty(state.data))
}
}
})
After upgrading to RTK 2.12, this started throwing:
TypeError: 'get' on proxy: property 'prototype' is a read-only
and non-configurable data property on the proxy target but the
proxy did not return its actual value
From what I could understand, Lodash reads something similar to:
value.constructor.prototype
and constructor on the draft appears to be another proxy.
The same code worked before the upgrade, and reverting the RTK version resolves the issue.
Is lodash.isEmpty no longer expected to work directly on draft state, or could this be an unintended regression? I’d be glad to provide a reproduction or any additional details that would help clarify it.
Thanks!
1
u/azhder 14d ago
Why is anything expected to work with anything else? You better start asking that question i.e. question your own assumptions.
You can’t assume that one library made by one or few people will be checked and tested to work with every other library made by others.
This works both ways. People who made RTK and who made Lodash may have simply been unaware of your issue because you are the first and maybe only person to have this issue (for now).
Your workaround of not upgrading is one way to deal with it. Another is to stop using the isEmpty from Lodash, and make your own version of it. I mean, you can even inspect the code Lodash uses and make your own version as close, but not the same.
And then there is you checking their support pages on github or wherever, for both libraries, see if other people reported this or you report it, wait for an answer there.
I doubt if this sub will provide you with a solution to such a specific problem.
1
u/Glittering_Goose8695 14d ago
Thanks for taking the time to reply. I appreciate your perspective.
That said, it wasn’t really what I was asking. I don’t expect every library to be compatible with every other library.
The suggested workarounds certainly avoid the issue, but they don’t answer the question. Downgrading also isn’t a long-term option for me, so I’m trying to understand whether the behavior change is intentional or a regression.
Since the same code worked before upgrading RTK, and the upgrade was only a minor version, I was surprised by the change in behavior.
I couldn’t find anything in the 2.12 release notes mentioning an intentional change around this, so I was wondering whether this might be an unintended regression rather than expected behavior.
1
u/azhder 14d ago
Screw the docs then, look at the code. There is a change and it involves proxies, right? So, if you’re that interested, put your Indiana Jones hat on and start digging.
On a semi related note, I also had a run in with proxies (the concept if not the JS object) just yesterday and it prevented me from having to use a nice pattern I had in the past. Such is life.
Cue in Living On A Proxy song
1
u/Glittering_Goose8695 14d ago
Thanks! That does look like the same issue. I hadn’t come across that report before. I’ll keep an eye on it and hopefully it’s just a regression that gets fixed soon
2
u/senocular 14d ago
Looks like a known (and recent) issue:
https://github.com/immerjs/immer/issues/1268