getResources
Returns an array or object of resources from resourceSlice based on the filter provided.

Arguments

  1. 1.
    resourceSlice (Object): The slice of your state that a resourceReducer is responsible for.
  2. 2.
    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, then getResources will iterate over the collection of resources, returning the resources that the function returns truthy for. The function will be called with three arguments: (resource, resourceMeta, resourceSlice). If no filter is provided, then all of the resources in the resourceSlice will be returned.
  3. 3.
    options (Object): An object to customize the behavior of getResources. Presently, only one option is supported: byId. Pass { byId true } to receive the results as an object instead of an array.

Returns

(Array|Object): An Array of resources, unless byId is passed as true, in which case an object will be returned instead.

Example

1
import { getResources } from 'redux-resource';
2
import store from './store';
3
4
const state = store.getState();
5
6
// Retrieve resources by an array of IDs
7
const someBooks = getResources(state.books, [1, 12, 23]);
8
9
// Retrieve those same resources as an object
10
const someBooksAsObject = getResources(state.books, [1, 12, 23], { byId: true });
11
12
// Retrieve resources by a list name
13
const popularBooks = getResources(state.books, 'mostPopular');
14
15
// Retrieve the "selected" resources
16
const selectedBooks = getResources(state.books, (resource, meta) => meta.selected);
17
18
// Returns all resources
19
const allResources = getResources(state.books);
Copied!

Tips

  • You don't always need to use this method to access resources. Just need one resource? If the resource is on a slice called books, you can directly access it using store.getState().books.resources[bookId].
  • When the order of your resources doesn't matter, then it probably makes sense to pass { byId: true } as options so that you can look up your resources more quickly.