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.

Immutable drafts with immer

See original GitHub issue

Describe the bug

Hi! I am trying to use simpler-state with immer and TypeScript in React Native and I am not able to mutate the document draft. Here’s the relevant code:

interface State {
  readonly plants: [
    {
      readonly name?: string,
    }?
  ]
};

const initialState: State = {};

export const state = entity(initialState);

export function addPlant() {
  state.set(produce(draft => {
    draft.plants = draft.plants || [];
    draft.plants.push({});
  }));
}

This code results in the following error:

Unhandled promise rejection: TypeError: Attempted to assign to readonly property.]
at node_modules/immer/dist/immer.esm.js:1:15732 in <anonymous>
at node_modules/immer/dist/immer.esm.js:1:16123 in f.then$argument_1
at node_modules/immer/dist/immer.esm.js:1:15694 in <anonymous>
at node_modules/simpler-state/lib/entity.js:40:49 in createSetter
at node_modules/simpler-state/lib/persistence.js:26:16 in <anonymous>
at node_modules/react-native/Libraries/Pressability/Pressability.js:691:17 in _performTransitionSideEffects
at node_modules/react-native/Libraries/Pressability/Pressability.js:628:6 in _receiveSignal
at node_modules/react-native/Libraries/Pressability/Pressability.js:524:8 in responderEventHandlers.onResponderRelease
at [native code]:null in forEach
at [native code]:null in callFunctionReturnFlushedQueue

I am trying to initialize the plants array if it doesn’t already exist.

To Reproduce

Run addPlant above.

Expected behavior

I would expect the whole draft document to be mutable.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ottobonncommented, Jun 18, 2021

Hi again! I just figured it out. I was using the persistence option with SimpleR State to save the JSON to the filesystem, but I didn’t notice that it handles stringification for me. As a result, I was double-serializing the JSON saved to the filesystem, and when I loaded it I wasn’t deserializing it, so it was a string instead of an object. When I tried to mutate it with immer, I was actually trying to mutate a string, so it was throwing an error.

Thanks for the reply and for the great library!

0reactions
arnelenerocommented, Jun 18, 2021

Thanks for posting the details. This could help others who, in the future, might also run into a similar scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to Immer - GitHub Pages
Using Immer is like having a personal assistant. The assistant takes a letter (the current state) and gives you a copy (draft) to...
Read more >
Using Immer with React: a Simple Solutions for Immutable ...
Immer is a small library created to help developers with immutable state based on a copy-on-write mechanism, a technique used to implement a ......
Read more >
Immutability in React with Immer - LogRocket Blog
Immer will copy your actual state data, and create a new temporary “draft” of it. This draft will be a proxy of the...
Read more >
Better Reducers With Immer - Smashing Magazine
In this article, we're going to learn how to use Immer to write reducers. When working with React, we maintain a lot of...
Read more >
Immutability with immer - madewithlove
Immutability in Javascript is complicated. This library called Immer can help you easily create and update data in an immutable way.
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