question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object not extensible when referencing old recoil value in socket.io callback

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
drarmstrcommented, Jun 10, 2020

@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.

4reactions
pnkflydgrcommented, Jul 28, 2020

Just for thoroughness I want to share what I found is a better approach than JSON.parse() for deep cloning.

import { cloneDeep } from "lodash"
...
const [stateObj, setStateObj] = useRecoilState(objAtom)
const nestedChangeHandler = ((...) => {
  let clonedStateObj = cloneDeep(stateObj)
  // update nested properties of clonedStateObj
  setStateObj(clonedStateObj)  
}

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found