Uncaught TypeError: Cannot freeze
See original GitHub issueWhen 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:
- Created 3 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >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
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.You can use objects, but you can expect lesser errors when using primitive ones.