createResource

Creates a resource and associated actions and selectors to interact with it

Arguments

  1. (resourceName): (string) The name under which the resource will be stored in the state

  2. (options): (object) An object containing additional, optional options for the resource:

    A. (cacheLifetime): (number, default: 0) The duration (in seconds) for which the resource will be considered valid (from 0 - no cache - to Infinity - cached permanently)

    B. (denormalizer): ((resourceIds, resources) : array<resources>, default null) A function useful to denormalize nested objects which have been normalized by actionName.normalizer (e.g. via normalizr)

  3. (actions): (map<actionName:config>) An object of configs with the following attributes:

const resource = {
    // Mandatory
    method: 'GET|POST|PATCH|PUT|DELETE',
    url: string || func,
    // Optional
    beforeHook: func,
    normalizer: func,
    metadataNormalizer: func,
    afterHook: func,
    // Also optional. Override the built-in network helpers
    // and the ones you may have provided using initializeNetworkHelpers
    networkHelpers: {
      getToken: func,
      requestGET: func,
      requestPATCH: func,
      requestPUT: func,
      requestPOST: func,
      requestDELETE: func,
      handleStatusCode: func,
      handleError: func,
    },
  },
  ...
};

Extensive documentation on actions configuration can be found here.

Returns

(Object): An object containing actions and selectors.

Example

This example demonstrates how to use createResource to create a sample resource, and then to export the reducers, actions and selectors.

Tips

  • The above example is very exhaustive, but you can export only what you really need

  • Naming is a matter of personal preference, use what works for you

  • You can alternately just export users, and spare yourself the trouble of mapping the names. Then just use the selectors like so: users.selectors.resource.getResource(). Again, use what works for you

Last updated