# Creating a copy of the original data then calling setState()?

**URL:** <https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331>\
**Category:** React\
**Created:** [April 21, 2021, 10:39pm UTC](https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331 "2021-04-21T22:39:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sollazzo08](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sollazzo08](https://forum.codewithmosh.com/u/sollazzo08)\
**Post date:** [April 21, 2021, 10:39pm UTC](https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331/1 "2021-04-21T22:39:10Z")

</div>

Why do we create copies of data or objects for example:

 ![Screen Shot 2021-04-21 at 6.38.09 PM](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/0/0e259b6e274b7c26467b163cfeca7265f62b4f93.png)

Why do we do this and not just call setState() on the original?

Should I be doing this for every obj or data I am dealing with?

---

<div class="post-metadata">

**Author:** ![sollazzo08](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@sollazzo08](https://forum.codewithmosh.com/u/sollazzo08)\
**Post date:** [April 22, 2021, 12:06am UTC](https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331/2 "2021-04-22T00:06:04Z")

</div>

Ok so I did some googling and well I found this very nice stackoverflow answer

[https://stackoverflow.com/questions/37755997/why-cant-i-directly-modify-a-components-state-really](https://stackoverflow.com/questions/37755997/why-cant-i-directly-modify-a-components-state-really)

The second answer is the best explanation.

If anyone has any other input, I’d still love to hear it!

---

<div class="post-metadata">

**Author:** ![rob](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@rob](https://forum.codewithmosh.com/u/rob)\
**Post date:** [April 22, 2021, 3:32am UTC](https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331/3 "2021-04-22T03:32:46Z")

</div>

> Should I be doing this for every obj or data I am dealing with?

You should create a copy of anything in the state that you are going to change. In your example, I assume counters is a property in the component’s state. In this case, you would need to copy both the counters array and the counter object because both of those objects are changed in the code you provided. The counters array is changed when you’re inserting a new object. When you make a copy of the counters array, you have a new array but it still points to all the original objects. Since you want to also decrement the value of the counter object, you need to make a copy of that as well.

---

<div class="post-metadata">

**Author:** ![Kodertian](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/kodertian/32/1351_2.png) [@Kodertian](https://forum.codewithmosh.com/u/Kodertian)\
**Post date:** [April 22, 2021, 5:57am UTC](https://forum.codewithmosh.com/t/creating-a-copy-of-the-original-data-then-calling-setstate/4331/4 "2021-04-22T05:57:49Z")

</div>

In simple words because updating the state is more than just set the new values to your current state, there are other ‘jobs’ that must be done and `setState` will do that for you under the hood, otherwise you would have to do yourself.

[React Docs \> setState](https://reactjs.org/docs/react-component.html#setstate)
