loading...

YPSKA #12 Immer

github logo ・1 min read

You should probably know about... (17 Part Series)

1) YSPKA #1: Pulumi 2) You should probably know about... 3 ... 15 3) YSPKA #2: gRPC 4) YSPKA #3: Dapr 5) YSPKA #4: Plop JS 6) YPSKA #5 Next.js 7) YPSKA #7 Static site generators 8) YPSKA #8 Headless CMS 9) YPSKA #9 Svelte 10) YPSKA #10 Runtypes 11) YPSKA #11 GROQ 12) YPSKA #12 Immer 13) YPSKA #13 Rollup 14) YPSKA #14 Pupeteer 15) YPSKA #15 Curl 16) YPSKA #15 Taking a break 17) YPSKA #16 lit-html

Immutability is kind of hard in JavaScript, at least it takes a bit of work to make it happen.

To make it easier, there's of course a library for that, Immer. Immer uses something called producers to update state and also keep that new state immutable. The basic idea is like this:

const newState = produce(oldState, draft => {
   // modify state
});

So you pass in the old state and a function that takes a "draftState" and makes modifications to it. The returned state is really a read-only object produced by Object.freeze().

If you need immutability in your JavaScript/TypeScript, check out Immer.

Less than ten minutes margin from missing a day in this series ;-)

twitter logo DISCUSS
Classic DEV Post from Apr 1 '19

Tell me about a time you messed up

So I brought down the site for a little while this morning. Now I'm interested ...

Håkan Fahlstedt profile image
Senior fullstack developer that loves technology and sharing knowledge.

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️