Object not extensible when referencing old recoil value in socket.io callback
See original GitHub issueIn a Socket.io callback, I am granular-ly updating a property of a Recoil state object.
I’m doing something similar to
const [toUpdate, setToUpdate] = useRecoilState(someAtom)
socket.on("this_event", (data: any) => {
setToUpdate((toUpdate: any) => {
toUpdate[someArray].push(data)
return toUpdate
})
})
or the syntax
setToUpdate((toUpdate: any) => {
const newToUpdate = Object.assign({},toUpdate)
newToUpdate[someArray].push(data)
return newToUpdate
})
both throw an “object not extensible” error. This sytnax works fine in vanilla React state and was wondering if this is a bug or if there’s a workaround to setting a property of a Recoil state?
I’ve been able to get a temporary workaround by doing
setToUpdate((toUpdate: any) => {
const newToUpdate = JSON.parse(JSON.stringify(toUpdate))
newToUpdate[someArray].push(data)
return newToUpdate
})
since it completely copies the contents, but it is very hacky and in the long term very slow
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
Object is not extensible error when creating new attribute for ...
data when calling the function), but this didn't seem to be a problem as newData is a new copy of data array... I'm...
Read more >Twisted Documentation - Read the Docs
This document describes the optional dependencies that Twisted supports. The dependencies are python packages that.
Read more >news.txt - Desastres (CIDBIMENA)
(Rob) - Fixed bug #27764 (Get return value from a stored procedure not returning any ... (Ilia) - Improved the engine to use...
Read more >EvT - ALBA.Net
Lubelskie linie autobusowe pks, Mario oscar salvucci, The profligates myspace, Gato cafe decebal, Juan maria rosasco, Taotao 110cc atv no spark, ...
Read more >the of and to a in for is on s that by this with i you it not
the of and to a in for is on s that by this with i you it not or be are from at...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@quuu - Objects stored in Recoil state are frozen by default. We want to ensure that we are capturing all “state changes” to properly update other nodes in the data-flow graph or React components which depend on that state. However, in some cases it may make sense to override this, which you can do so with the
dangerouslyAllowMutability
option in the atom or selector.Just for thoroughness I want to share what I found is a better approach than JSON.parse() for deep cloning.