redux-json-api-helper
A package for consuming and accessing JSON API data with Redux. It will take a standard JSON API response, automatically flatten its structure, and update your redux store. For most apps, this greatly cuts down on the number of reducers that need to be written.
Note: This package does not make/handle network requests. Its purpose is to consume JSON API data that has already been retrieved from a server.
Hooking up the store
;; const store = ;
Consuming JSON data
; ;
Manipulating entities
; /** * dispatch(updateEntity('article', articleId, { * isUserFavorite: true * })); */; /** * dispatch(addRelationshipToEntity('article', '54321', 'readers', { * type: 'user', * id: '12345', * attributes: { name: "Bob Ross" } * })); */; /** * You can also add relationships by ID. * dispatch(addRelationshipToEntity('article', '54321', 'readers', '12345')); */; // dispatch(removeRelationshipFromEntity('article', '54321', 'readers', '12345'));;
Retrieving entities from the store
; // Get single articleconst article = ; // Get all articlesconst articles = ; // Get array of articlesconst articles = ;
Removing entities from the store
; // Remove a single entity; // Remove all entities from an entity type;
Metadata
; // Set a metadata value for an Entity type; // Get all metadata for an Entity typeconst metadata = ; // Get a specific metadata value for an Entity typeconst isLoading = ; // Set a metadata value for a specific Entity; // Get all metadata for a specific Entityconst metadata = ; // Get a specific metadata value for a specific Entityconst isLoading = ;
Helpers
; // Extract item ID from JSON API response; // Extract collection ID's from JSON API response;
Generate an entity locally
Sometimes you may need to generate and store an entity that didn't actually come from a JSON API.
redux-json-api-helper provides a simple generateEntity
helper function for that.
; // Generate an Article entity and store it// generateEntity(entityKey, attributes);const article = ;; // If no ID is provided, one will be generated automatically using UUID v4const user = ;;