How to Delete an Item in a Nested Object in Redux State
Tags: redux
Published: 2020-05-17 09:00:01

Your Redux state can get complex, here’s how to remove nested data

![Photo by [Lysander Yuen](https://unsplash.com/@lysanderyuen?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/complex?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/8414/1*v_shUfxgcMhfVlwM1zBFJQ.jpeg)*Photo by [Lysander Yuen](https://unsplash.com/@lysanderyuen?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/complex?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*

One of the key features of [Redux](https://redux.js.org/) is its [store](https://redux.js.org/api/store/); it holds the logic of your application as a state object.

In essence, you get a limited number of methods that can be triggered in the store. For example, use [getState](https://redux.js.org/api/store/#getstate) as a mechanism for reading the current state tree.

You might ask: “What about writing to state?”

Well, that’s where [dispatch(action)](https://redux.js.org/api/store/#dispatchaction) comes in. This is the only way in Redux to trigger state change. Dispatching actions are tied closely with the reducer concept. The reducer receives both action and state and returns a new application state.

The golden rule is that we do not return a mutated state, but rather a new state. Depending on the type of your action, you might need to update your state tree in various forms when it hits the reducer.

* You might need to add a new item to one of the state properties.

* There might be a need to update or re-assign a state property.

* Or you can have a scenario where you need to remove an item from a state property.

This brings us to the concept of [Redux’s immutable update (or data modification) patterns](https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns/). Immutability is key because we never want to directly change a value in the state tree, but rather always make a copy and return a new value based on the old value.

State properties can have many different types. But when we talk about the challenges of data modification patterns, Array and Object can be considered the ones with more edge cases.

In this article, we are going to focus on a specific Redux data modification pattern that concerns deleting an item in a nested object on a state tree.

---

**Tip**: I completely understand that software interviews can be a bit scary, so my hope is to give you clear ideas about the interview process and offer you practical advice on how to do well at each step.

![Map](https://cdn-images-1.medium.com/max/1600/0*rLNMa6aQpwnMNARC.png)

This course can be very helpful for you to get an overview of all the common interview steps that companies go through to hire a developer. [Sign up for SkillShare, Get two months of free trial and Join me on this journey](https://skl.sh/3dxx6Bp)

---

## Use Case: Deleting an Item in a Nested Object in Redux State

Let’s imagine we have a fashion e-commerce application with the following initial state.

<iframe src="https://medium.com/media/db96411612222ec64ca3201f8055f110" frameborder=0></iframe>

The state tree is simple and includes a few object keys like outfits or filters.

In our application, we have a filtering mechanism that filters outfits based on attributes like brand** **or colour. For instance, if the user is interested in seeing outfits of Bally brand with Black colour and does the filtering; that will result in the following state tree.

<iframe src="https://medium.com/media/b18d77158cf860e2256b3a1a28160f03" frameborder=0></iframe>

If we want to have a more user-friendly application, we might be interested in displaying the filters that the user has selected; right below the filtering system.

In this way, the user has the option to see all of the filters they have chosen and can remove the filters they are no longer interested in.

![](https://cdn-images-1.medium.com/max/2304/1*366_aUREBWuC5dgUcAHF8w.png)

For achieving this goal, we need to have a removeFilter** **actionCreator** **that takes the field type (for example, brand) and index (for instance, the first element) in the displayed filters and removes that particular filter value from the corresponding element in the state tree.

<iframe src="https://medium.com/media/28fdf4bd25056af7de7234298970f435" frameborder=0></iframe>

The below code shows the rendering part. The buttons are rendered by iterating over a filtered list which renders Buttons and provides the index and the field to them.

<iframe src="https://medium.com/media/565c57e3acb88650510df88151d221d4" frameborder=0></iframe>

---

## **Repo**

If you’re interested, [here’s a repo](https://github.com/kasrakhosravi/redux-update-patterns) that better shows the use case we covered in this article.

---

**Tip**: I completely understand that software interviews can be a bit scary, so my hope is to give you clear ideas about the interview process and offer you practical advice on how to do well at each step.

![Map](https://cdn-images-1.medium.com/max/1600/0*rLNMa6aQpwnMNARC.png)

This course can be very helpful for you to get an overview of all the common interview steps that companies go through to hire a developer. [Sign up for SkillShare, Get two months of free trial and Join me on this journey](https://skl.sh/3dxx6Bp)

---