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.

Uncaught TypeError: Cannot freeze

See original GitHub issue

When using the setter function to update a value of an atom you cannot pass any object as Object.freeze(value) can throw an error in some cases.

In my case I am passing a Firebase user to update the value of an atom but I am having Uncaught TypeError: Cannot freeze error. The error happens here: https://github.com/facebookexperimental/Recoil/blob/e018c3abfb68f559bf493ba244079fa73f89e79b/src/util/Recoil_deepFreezeValue.js#L65

Example

const userState = atom({
  key: 'userState',
  default: null
});

const [user, setUser] = useRecoilState(userState);

auth.onAuthStateChanged(fetchedUser => {
    setUser(fetchedUser);
});

Another example would be that passing something like location.ancestorOrigins will also throw the same error.

What would be the solution? Should I just take some of the properties and create a new simpler object and pass it to the setter function or somehow convert the entire user object into a freezable object?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
drarmstrcommented, Jun 26, 2020

Recoil freezes stored values for atoms in part to help ensure that all state changes are visible and propagated as updates through the data-flow graph. If stored values are mutated in an attempt to change state it won’t be tracked or update dependencies. To help catch this, we deep freeze objects in development. However, there are situations where mutable objects may need to be used as values that don’t logically represent Recoil state changes. You can use the dangerouslyAllowMutability option when constructing your atom to allow for this.

2reactions
cybervaldezcommented, Jun 26, 2020

You can use objects, but you can expect lesser errors when using primitive ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular - "TypeError: Cannot freeze" when dispatching an action
It appears you are using the ngrx-store-freeze npm package. This package ensures that any updates you make to the store are not mutated ......
Read more >
Object.freeze() - JavaScript - MDN Web Docs
A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed, their enumerability, ...
Read more >
Cannot freeze" when dispatching an action-angular.js
It appears you are using the ngrx-store-freeze npm package. This package ensures that any updates you make to the store are not mutated...
Read more >
JavaScript object immutability: Object.freeze vs. Object.seal
Freezing defends against code attempting to mutate objects that should not be modified directly. Frozen or sealed objects can also prevent the ...
Read more >
Object.freeze() JavaScript - Scaler
The freeze() is a method of the Object class in JavaScript that ... Uncaught TypeError: Cannot assign to read only property 'device' of ......
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