ArticleZip > How Do You Add Remove To A Redux Store Generated With Normalizr

How Do You Add Remove To A Redux Store Generated With Normalizr

Redux is a popular tool used in many web applications to manage state effectively. One question that often comes up is how to add and remove items to a Redux store that has been set up using Normalizr, a handy library for managing normalized data in JavaScript.

Before we dive into the specifics of adding and removing data, let's quickly go over what Normalizr is and how it works in conjunction with Redux. Normalizr helps organize complex nested data structures into a more flat and normalized form. This can make your data management more efficient and easier to work with in Redux.

To add an item to a Redux store generated with Normalizr, you first need to dispatch an action to update the state with the new item. Suppose you have normalized data that looks something like this:

Js

{
  entities: {
    users: {
      1: { id: 1, name: 'Alice' },
      2: { id: 2, name: 'Bob' }
    },
    posts: {
      1: { id: 1, title: 'Hello, World!', author: 1 }
    }
  },
  result: [1]
}

In this example, we have users and posts entities with their corresponding IDs. If you want to add a new post, you would follow these steps:

1. Create a new post object with a unique ID and other necessary data.
2. Dispatch an action that includes the new post object.
3. In your reducer, handle the action by updating the entities and result arrays with the new post.

Here's a simplified example of how you could handle adding a new post in your reducer:

Js

case ADD_POST:
  const { id, title, author } = action.payload;
  return {
    ...state,
    entities: {
      ...state.entities,
      posts: {
        ...state.entities.posts,
        [id]: { id, title, author }
      }
    },
    result: [...state.result, id]
  }

Removing an item from a Redux store generated with Normalizr follows a similar process. You would dispatch an action to remove the item and update the state accordingly. For instance, to remove a post with ID 1:

1. Dispatch an action with the ID of the post you want to remove.
2. In your reducer, filter out the post with the given ID from the entities and result arrays.

Here's a basic example of how you could handle removing a post in your reducer:

Js

case REMOVE_POST:
  const postIdToRemove = action.payload;
  const { [postIdToRemove]: removedPost, ...posts } = state.entities.posts;
  const updatedResult = state.result.filter(id => id !== postIdToRemove);

  return {
    ...state,
    entities: {
      ...state.entities,
      posts,
    },
    result: updatedResult
  }

In summary, adding and removing items to a Redux store generated with Normalizr involves understanding how to update the entities and result arrays in your state based on the actions you dispatch. With the right setup and understanding of how Redux and Normalizr work together, you can efficiently manage your application's state in a normalized way.