# State mutation made simple — A JavaScript journey #1

> When your JavaScript application gets bigger, you’ll quickly need something like Redux to distribute data across your application and your components. With Redux or any kind of state management library, \[…\]

Source: https://antistatique.net/en/blog/state-mutation-made-simple-a-javascript-journey-1

Category: Development

# State mutation made simple — A JavaScript journey #1

Author(s)

[

![Yann](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Yann-G-300x300.jpg)



](/en/agence/yann)

[Yann](/en/agence/yann)21 September 2018

When your JavaScript application gets bigger, **you’ll quickly need something like [Redux](https://redux.js.org/) to distribute data across your application and your components**. With Redux or any kind of state management library, the idea is to trigger some actions with a _payload_, which will themselves alter the global data storage of the app using this portion of new data.

It’s fairly easy to setup when you deal with simple data structure. **For example**, if you want to update your `content` at your Store’s root, you can do something like:

```
const reducer = (state = {}, action) => {
  if (action.type === "ALTER_STORE") {
    return {
      ...state,
      content: action.payload
    };
  }
  return state;
};
```

[📦 Play with the CodeSandbox example!](https://codesandbox.io/s/rlo00m8lwn)

But when you’re starting to have a more complex data structure with nested objects and arrays, things became quickly tricky and after the Callbacks and the Promises of Hell, please welcome the “_Reducer of Hell_” (thanks to Object Spread goodness)!

**For example** if your want to update a specific post’s `content` attribute:

```
const complexDefaultState = {
  contents: {
    post: [
      { id: 0, type: "post", content: "First article" },
      { id: 1, type: "post", content: "Second article" },
      { id: 2, type: "post", content: "Third article" }
    ],
    page: []
  }
};

const reducer = (state = complexDefaultState, action) => {
  if (action.type === "ALTER_STORE") {
    return {
      ...state,
      contents: {
        ...state.contents,
        [action.payload.type]: [
          ...state.contents[action.payload.type].slice(0, action.payload.id),
          {
            ...state.contents[action.payload.type][action.payload.id],
            ...action.payload
          },
          ...state.contents[action.payload.type].slice(action.payload.id + 1)
        ]
      }
    };
  }
  return state;
};
```

[📦 Play with the CodeSandbox example!](https://codesandbox.io/s/64k1rzpyjk)

Dirty, right? Ok, maybe you can be smarter and start modeling something more decoupled and less nested, or maybe not… Thankfully, **Michel Weststrate**, the smart guy behind [MobX](https://github.com/mobxjs/mobx), had provided us a more elegant solution for making state mutations: [Immer.js](https://github.com/mweststrate/immer)!

You simply have to wrap your reducer inside the `immer` method and replace your dirty mutation code by a more elegant one, same as native `Array` and `Object` prototype methods.

**For the same complex example** using Immer, your reducer will look like:

```
import p from "immer";

const complexDefaultState = {/* see previous example */};

const reducer = p((state = complexDefaultState, action) => {
  if (action.type === "ALTER_STORE") {
    state.contents[action.payload.type][action.payload.id].content =
      action.payload.content;
  }
  return state;
});
```

[📦 Play with the CodeSandbox example!](https://codesandbox.io/s/047y582040)

Holy cow! Yep, my first reaction too. **Now you can handle complex Store data without having dirty reducers**. Complex data; clean, typed and performant reducers, what else?

I invite you to read all the good stuff of Immer.js on it [repository](https://github.com/mweststrate/immer). There is a great and visual explanation of how it works under the hood and what are all the benefits of using it (aside of reducing your code size by 1,000).

## More articles

Author(s)

[

![Anthony](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2024/11/anthony-1-300x300.png)



](/en/agence/anthony-salamone)

[Anthony](/en/agence/anthony-salamone)28 September 2026

### [Smashing Conference Freiburg 2026](/en/blog/smashing-conference-freiburg-2026)

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

Back in Freiburg for Smashing Conference 2026: AI and design systems, deeply technical CSS and HTML, and more human talks about teams and mindset. Here's what we took away.

[

![Smashing Conference Freiburg 2026 poster](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/assets/blog/smashing-2026.png)



](/en/blog/smashing-conference-freiburg-2026)

Author(s)

[

![Akira](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2025/03/akira-small-300x300.jpg)



](/en/agence/akira-kamata)

[Akira](/en/agence/akira-kamata)22 July 2026

### [Making of : “La nuit des Losers”](/en/blog/making-of-la-nuit-des-losers)

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

lanuitdeslosers.ch, built fast: paper wireframes, a bold hand-drawn identity, then Claude Code to prototype and ship. A mouse-tracking hero animation, a bowling game made of sponsor logos, and a few bugs we’re keeping.

[

![Making of : “La nuit des Losers”](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/05/La-nuit-des-Losers-Teaser-1-768x576.png)



](/en/blog/making-of-la-nuit-des-losers)

Author(s)

[

![Pierre](https://images.statique.ch/t/w_750,h_750,c_fill,f_jpeg,q_70/wp/app/uploads/2020/12/Pierre-300x300.jpg)



](/en/agence/pierre)

[Pierre](/en/agence/pierre)7 July 2026

### [Dark mode: necessity or trend](/en/blog/dark-mode-necessity-or-trend)

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

Nous faisons le tri entre idées reçues, réalités techniques et bonnes pratiques de design.

[

![Dark mode: necessity or trend](https://images.statique.ch/t/w_750,h_500,c_fill,f_jpeg,q_70/wp/app/uploads/2026/07/1-768x384.png)



](/en/blog/dark-mode-necessity-or-trend)
