# Write a Counter with Redux: Solution code mutating state?

**URL:** <https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [August 2, 2020, 8:07pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157 "2020-08-02T20:07:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [August 2, 2020, 8:07pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/1 "2020-08-02T20:07:11Z")

</div>

Is the solution code shown not mutating the state?

> [@freeCodeCamp Challenge Guide: Write a Counter with Redux](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-write-a-counter-with-redux/301453):
>
> [Write a Counter with Redux](https://www.freecodecamp.org/learn/front-end-libraries/redux/write-a-counter-with-redux)Problem Explanation The goal of this challenge is to apply all the concepts learned from previous Redux challenges and create a simple counter that will increment or decrement a state by 1. Hints Hint 1 The best way to solve this challenge is by reviewing previous redux lessons particularly the following: [Use const for Action Types](https://learn.freecodecamp.org/front-end-libraries/redux/use-const-for-action-types/)[Use a Switch Statement to Handle Multiple Actions](https://learn.freecodecamp.org/front-end-libraries/redux/use-a-switch-statement-to-handle-multiple-actions/)[Define an Action Creator](https://learn.freecodecamp.org/front-end-libraries/redux/define-an-action-creator/)[Handle an Action in the Store](https://learn.freecodecamp.org/front-end-libraries/redux/handle-an-action-in-the-store/)[Create a Redux Store](https://learn.freecodecamp.org/front-end-libraries/redux/create-a-redux-store)Soluti…

I don’t really use Redux, but in the reducer should it not just be:

```auto
return state + 1
return state - 1

```

And not:

```auto
return (state += 1);
return (state -= 1);

```

Just like it is in the counter example from the redux page

> **[Counter​ - Examples | Redux](https://redux.js.org/introduction/examples#counter)**
>
> Redux is distributed with a few examples in its source code. Most of these examples are also on CodeSandbox, an online editor that lets you play with the examples online. | Introduction \> Examples: Redux interactive example apps

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [August 5, 2020, 7:52pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/2 "2020-08-05T19:52:18Z")

</div>

I will update the solution code unless someone can explain to me why it should be

`return state += someValue`

and not just

`return state + someValue`

Obviously, if this is wrong, I will put it back to how it was before.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [August 5, 2020, 7:57pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/3 "2020-08-05T19:57:42Z")

</div>

Sorry, for missing this until now. But, you are absolutely correct in changing it. At the end of the day, it does not actually change anything, because, even though one should not mutate state, a new state will be returned. So, they both accomplish the same thing, but not mutating is better.

Nice catch.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [August 5, 2020, 8:40pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/4 "2020-08-05T20:40:23Z")

</div>

> [@Sky020](#):
>
> At the end of the day, it does not actually change anything, because, even though one should not mutate state, a new state will be returned. So, they both accomplish the same thing, but not mutating is better.

Isn’t it just less of a problem when the state is a primitive value, i.e. in this case a number which is an immutable value, and not an object?

Again, I’m not a Redux expert but I thought mutating state had the possibility of actually breaking things?

> [Note on immutability, side effects, and mutation](https://redux.js.org/recipes/structuring-reducers/prerequisite-concepts#note-on-immutability-side-effects-and-mutation)
> 
> Mutation is discouraged because it generally breaks time-travel debugging, and React Redux’s `connect` function:
> 
> - For time traveling, the Redux DevTools expect that replaying recorded actions would output a state value, but not change anything else. Side effects like mutation or asynchronous behavior will cause time travel to alter behavior between steps, breaking the application.
> 
> - For React Redux, `connect` checks to see if the props returned from a `mapStateToProps` function have changed in order to determine if a component needs to update. To improve performance, `connect` takes some shortcuts that rely on the state being immutable, and uses shallow reference equality checks to detect changes. This means that changes made to objects and arrays by direct mutation will not be detected, and components will not re-render.
> 
> Other side effects like generating unique IDs or timestamps in a reducer also make the code unpredictable and harder to debug and test.

> [Why is immutability required by Redux?](https://redux.js.org/faq/immutable-data#why-is-immutability-required-by-redux)
> 
> - Both Redux and React-Redux employ [shallow equality checking](https://redux.js.org/faq/immutable-data#shallow-and-deep-equality-checking). In particular: - Redux’s `combineReducers` utility [shallowly checks for reference changes](https://redux.js.org/faq/immutable-data#how-redux-uses-shallow-checking) caused by the reducers that it calls. - React-Redux’s `connect` method generates components that [shallowly check reference changes to the root state](https://redux.js.org/faq/immutable-data#how-react-redux-uses-shallow-checking), and the return values from the `mapStateToProps` function to see if the wrapped components actually need to re-render. Such [shallow checking requires immutability](https://redux.js.org/faq/immutable-data#redux-shallow-checking-requires-immutability) to function correctly.
> 
> - Immutable data management ultimately makes data handling safer.
> 
> - Time-travel debugging requires that reducers be pure functions with no side effects, so that you can correctly jump between different states.

I know you already agreed with the change so I’m not really sure what I’m after here, maybe I just wanted to check my understanding of Redux.

Anyway, even if it doesn’t break anything in this particular case, mutation in Redux would seem to be an anti-pattern at the very least.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [August 5, 2020, 8:47pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/5 "2020-08-05T20:47:43Z")

</div>

> [@lasjorg](#):
>
> Isn’t it just less of a problem when the state is a primitive value, i.e. in this case a number which is an immutable value, and not an object?

Well, now you have peeked my interest in the subject. From what I understand (very little), altering the state within the reducer should not cause anything to break, as the input to the dispatcher is measured against the output of the reducer. So, anything inbetween is moot?

I will do some research…

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [August 5, 2020, 9:00pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/6 "2020-08-05T21:00:47Z")

</div>

I agree that it should be changed for clarity, but I don’t think that it’s mutating state. Correct me if I’m wrong, but since it is a primitive, it is being passed in by value and a local instance of that primitive is created. Then that _value_ is returned and put onto the state. I think that _that_ is what is being changed. I think that both solutions do the exact same thing and neither mutates state, I do agree that `state += 1` is unnecessarily confusing.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [August 5, 2020, 9:32pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/7 "2020-08-05T21:32:25Z")

</div>

That was my initial assumption as well. No real mutation is taking place because of the value type being a primitive. I just don’t know enough about how Redux, it’s tooling, or middleware works behind the scene to fully say that reassigning the state can’t cause any issues.

But the idea behind making this change was just to make sure we are not teaching anything wrong or showing anti-patterns. And as you said, the reassignment seems unnecessary, if nothing else.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [August 5, 2020, 10:01pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/8 "2020-08-05T22:01:34Z")

</div>

Yeah, my understanding of redux is that both will work fine because you are not mutating the state because you are returning a whole new state. The fact that it is a primitive means that it is impossible to mutate. But yeah, the `+=` is confusing because it makes it seem like you’re mutating state.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:32pm UTC](https://forum.freecodecamp.org/t/write-a-counter-with-redux-solution-code-mutating-state/413157/9 "2021-06-01T18:32:37Z")

</div>

Closed on June 1, 2021, 6:32pm UTC


