getResources(resourceSlice, filter)
Returns an array of resources from resourceSlice
based on the filter
provided.
Arguments
resourceSlice
(Object): The slice of your state that aresourceReducer
is responsible for.filter
(Array|String|Function): The filter to apply. It can be an array of resource IDs, or the name of a list. If a function is provided, thengetResources
will iterate over the collection of resources, returning an array of resources that the function returns truthy for. The function will be called with three arguments:(resource, resourceMeta, resourceSlice)
.
Returns
(Array
): An Array of resources.
Example
import { getResources } from 'redux-resource';
import store from './store';
const state = store.getState();
// Retrieve resources by an array of IDs
const someBooks = getResources(state.books, [1, 12, 23]);
// Retrieve resources by a list name
const popularBooks = getResources(state.books, 'mostPopular');
// Retrieve the "selected" resources
const selectedBooks = getResources(state.books, (resource, meta) => meta.selected);
Tips
- You don't always need to use this method to access resources. Just need one
resource? If the resource is on the
books
slice, you can directly access it viastore.getState().books.resources[bookId]
.
Old Signatures
In v2 of Redux Resource, you may use the signature: getResources(state, resourceName, filter)
,
but this has been deprecated as of v2.2.0, and will be removed in v3.0.0.