jamesplease / redux-resource

3kb resource management for Redux
https://redux-resource.js.org
MIT License
237 stars 28 forks source link

[Question] How can I remove an id in lists #445

Closed tomzaku closed 4 years ago

tomzaku commented 4 years ago

i have a favourite list

{
lists: {
favorites: ["1","2"]
}
}

Now I would like to remove "1" out of my favourite list without delete the object "1" in resourses object. How can I handle this case

tomzaku commented 4 years ago

First, I tried to use this one

 const favoritesList = getState().gallery.lists.favorites as string[];

    dispatch(
      removeFavoriteActionCreators.succeeded({
        resources: favoritesList.filter(favoritesId => id !== favoritesId),
        mergeListIds: false,
        disableUpdatePagination: true
      })
    );
  };

Do you have any better solution?

jamesplease commented 4 years ago

Hi @tomzaku ! There is an action type UPDATE_RESOURCES which can be used to update just a list, without affecting the actual resource.

Using it may look like the following:

function removeFromFavorites(favoritesId) {
  const favoritesList = getState().gallery.lists.favorites;
  const filteredList = favoritesList.filter(id => is !== favoritesId);

  dispatch({
    type: ‘UPDATE_RESOURCES’,
    lists: {
      gallery: {
        favorites: filteredList
      }
    }
  });
}

You can read more about this action type, and a related action type, DELETE_RESOURCES, here:

https://redux-resource.js.org/resources/modifying-resources