# React, calling backend services

**URL:** <https://forum.codewithmosh.com/t/react-calling-backend-services/764>\
**Category:** Uncategorized\
**Created:** [November 19, 2020, 6:47am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764 "2020-11-19T06:47:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunde](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@kunde](https://forum.codewithmosh.com/u/kunde)\
**Post date:** [November 19, 2020, 6:47am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/1 "2020-11-19T06:47:36Z")

</div>

Updating a post. I know that objects in JavaScript are referenced type. The handleUpdate method, takes in a post object, in that lecturer, Mosh directly modified the object. Since objects are reference type, modifying this object directly modifies the state. The state of a component should never be directly modified. I thought we were supposed to clone the object, and update the cloned copy and then pass the modified copy to the setState method to update the state

---

<div class="post-metadata">

**Author:** ![thanidasi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/thanidasi/32/5339_2.png) [@thanidasi](https://forum.codewithmosh.com/u/thanidasi)\
**Post date:** [January 7, 2023, 9:38am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/2 "2023-01-07T09:38:04Z")

</div>

I have the same concern, I feel it should be a mistake as otherwise it does not make sense with the guideline that one should not directly change the state.

---

<div class="post-metadata">

**Author:** ![thanidasi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/thanidasi/32/5339_2.png) [@thanidasi](https://forum.codewithmosh.com/u/thanidasi)\
**Post date:** [January 7, 2023, 9:42am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/3 "2023-01-07T09:42:55Z")

</div>

@maverick @sufian.babri can you please share any idea that you might have regarding this?

For reference, following is the code snippet that the query is about:

```auto
handleUpdate = async post => {
		post.title = "UPDATED";
		await axios.put(apiEndpoint + '/' + post.id, post);
		const posts = [...this.state.posts];
		const index = posts.indexOf(post);
		posts[index] = { ...post };
		this.setState({ posts });
	};

```

I think it should be something like the following where we are not directly changing the state:

```auto
handleUpdate = async post => {
		let postToUpdate = {...post};
		postToUpdate.title = 'Updated';
		await axios.put(apiEndpoint + '/' + post.id, postToUpdate);
		const posts = [...this.state.posts];
		const index = posts.indexOf(post);
		posts[index] = postToUpdate;
		this.setState({ posts });
	};

```

---

<div class="post-metadata">

**Author:** ![thanidasi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/thanidasi/32/5339_2.png) [@thanidasi](https://forum.codewithmosh.com/u/thanidasi)\
**Post date:** [January 7, 2023, 9:52am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/4 "2023-01-07T09:52:01Z")

</div>

@Mosh @SAM

---

<div class="post-metadata">

**Author:** ![SAM](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sam/32/167_2.png) [@SAM](https://forum.codewithmosh.com/u/SAM)\
**Post date:** [January 7, 2023, 10:44am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/5 "2023-01-07T10:44:28Z")

</div>

If the `post` passed to `handleUpdate` is from the state then changing it’s title would directly change the state, yes.

In case you are unsure you can set a breakpoint after the suspicious command and examine the state.

---

<div class="post-metadata">

**Author:** ![thanidasi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/thanidasi/32/5339_2.png) [@thanidasi](https://forum.codewithmosh.com/u/thanidasi)\
**Post date:** [January 7, 2023, 11:15am UTC](https://forum.codewithmosh.com/t/react-calling-backend-services/764/6 "2023-01-07T11:15:19Z")

</div>

thanks @SAM. Yes, I did check and the state was getting affected (updated) by that command.
