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.

Setting an object value inside an atom returns "Cannot assign to read only property 'xxxxx' of object '#<Object>'"

See original GitHub issue

I am trying to edit a key of ‘activeCourse’ inside an object of an atom value. However, whenever i do so it returns the error:

Cannot assign to read only property 'activeCourse' of object '#<Object>'

The atom is called ‘trainingAtom’ and default value is an array of objects:

[
  {
     _id: '5fc42caa244ccadea8e2f263',
     activeCourse: 'r4r3545243j',
     activeDetail: null,
   },
   {
      _id: '5fc42caa244ceecadea8e2f263',
      activeCourse: null,
      activeDetail: null,
    },
]

I am using the following function within my component:

const [test, setTest] = useRecoilState(trainingAtom);

useEffect(() => {
        let _test = [...test];
        const index = _test.findIndex((val) => val._id === id);
        _test[index].activeCourse = 123456;
        setTest(_test);
}, [id]);

This function works if I use normal React state - but anything inside Recoil throws the error.
Note, I have also tried to use the set function inside a selector but it gives the same error.

What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
cbuvcommented, Dec 15, 2020

have you tried using updater instead of value? like this:

export const Test = ({id}: any) => {
  const [test, setTest] = useRecoilState(trainingAtom);

  useEffect(() => {
    setTest((old: any) => {
      let _test = [...old];
      const index = _test.findIndex((val) => val._id === id);
      //@ts-ignore
      _test[index].activeCourse = 123456;
      return _test;
    });
  }, [id, setTest]);
  return (<div>{JSON.stringify(test)}</div>);
}
1reaction
artygrandcommented, Aug 6, 2021

@drarmstr I trying to use updater as @cbuv suggests, but in my case, I have list of big deep nested objects. For example if I need just to change status in lots[].clients[].bid.status, I have to write like below to defrost whole [lot] object and avoid error

let lots= [...old];

...

let lot = {...lots[x]};
let client = {...lot.clients[y]};
let bid = {...client.bid}
bid.status = val as BidStatus;
client.bid = bid;
lot.clients = [...lot.clients];
lot.clients[y] = client;
lots[x] = lot;

...

return lots;

Should I use dangerouslyAllowMutability or it is possible in any other more simple way?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot assign to read only property 'url' of object '#<Object>'
Show activity on this post. I tried changing a key's value inside my object, but it seems like I have been mutating the...
Read more >
“TypeError: Cannot assign to read only property of object ' ...
ERROR TypeError : Cannot assign to read only property 'value' of object '[object Object]' ... 'Switch' is not exported from 'react-router-dom'. ch warning....
Read more >
Webpack: Cannot assign to read only property 'exports'
Hi, I need to use an external library crypto when running my k6 tests. I'm using the example here: https://github.com/k6io/k6-es6 However on ...
Read more >
Configuration - IGSS
Reference to the Application object. CurrentAreaName As String (Read Only). Return the name of the current area as selected in supervise. When running...
Read more >
Troubleshooting Compiler Errors - NetFort
An attempt was made to assign a value to a read-only system variable. ... Review the object class, and correct it to match...
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