Standard Resource
  • Introduction
  • Read Me
  • Introduction
    • Motivation
    • Core Concepts
  • Store
    • Creating a Store
  • Resources
    • Resource Data
    • Creating
    • Retrieving
    • Updating
    • Deleting
    • Using Computed Attributes
    • Schemas
  • Groups
    • Creating
    • Retrieving
    • Updating
    • Deleting
    • Sorted Groups
  • API Reference
    • createResourceStore
    • Store
  • Glossary
  • Changelog
Powered by GitBook
On this page
  • Returning the Group as an Object
  • Tips
  1. Groups

Retrieving

PreviousCreatingNextUpdating

Last updated 7 years ago

Use to access a group from the store:

store.getGroup('selectedBooks');

When you retrieve a group, you will be given the full resources back.

For instance, the above call may return:

[
  {
    id: 2,
    attributes: {
      name: 'The Lord of the Rings',
      publishYear: 1940,
    },
    meta: {},
    computedAttributes: {},
  },
];

If you pass a group name of a group that does not exist to getGroup, you will receive an empty array back:

store.getGroup('aGroupThatDoesNotExist');
// => []

Returning the Group as an Object

Although groups are stored as arrays, it is not always the case that the order of that array matters for your use case. In those situations, it may be preferable to get an object returned from the call to getGroup, rather than an array. The second argument to getGroup is an options object that allows you to specify this behavior:

store.getGroup('selectedBooks', {
  byId: true,
});

This would return an object like the following:

{
  2: {
    id: 2,
    attributes: {
      name: 'The Lord of the Rings',
      publishYear: 1940,
    },
    meta: {},
    computedAttributes: {},
  },
};

Tips

Although you can also use to retrieve a group by directly accessing the state tree, we do not encourage that approach.

store.getGroup()
store.getState()