Resources module provides easy way to cache your async data.
The workflow for caching is:
Pass data from storage to component Send HTTP request Update storage and React Component # Cache configResource config acceps cache
config to make it easy to configure caching data.
Copy {
cache : {
storage : localStorage | sessionStorage ;
refresh ? : boolean ;
key : string | ( globalData : CacheGlobalData , namespace : string , resourceConfig , actoinPayload ) => string
}
}
# storageYou can choose what storage to use localStorage
or sessionStorage
# localStorageSend /api/users/me
HTTP request and cache data in localStorage
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : 'localStorage' , key : 'profile' }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
# sessionStorageSend /api/users/me
HTTP request and cache data in sessionStorage
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : sessionStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
# refreshBoolean optional configuration.
By default, Resources
module will always send HTTP request to refresh cache data.
To make data static and disable refreshing make this option false
.
In this case, API request the data will be fetched only once.
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : sessionStorage , key : 'profile' , refresh : false }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
# keyYou can configure a key in Storage to cache data with key
config
# Static keySave data localStorage with profile
key
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
This configuration will store API data in localStorage
under profile
key.
Copy profile: '<profile_data>'
# Empty keyIn case there is no key
config, resources module will use namepace
as a cache key
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : 'localStorage' }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
This configuration will store API data in localStorage
under profile
key.
Copy profile: '<profile_data>'
Copy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : '/:accountId/favorite-workspaces' ,
cache : { storage : localStorage }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
This configuration will store API data in localStorage
under camelCased namespace
without dynamic paths.
Copy favoriteWorkspaces: '<profile_data>'
# Custom cache keyYou can pass a function to key
config to generate custom Storage key
Copy import { usePrefetchResource } from '@cranium/resource' ;
function key ( ) {
return 'my-key'
}
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : key }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
# Usage examples# usePrefetchResourceCopy import { usePrefetchResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data } = usePrefetchResource ( resourceConfig )
}
# useResourceCopy import { useResource } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data , fetch } = useResource ( resourceConfig )
useEffect ( ( ) => {
const request = fetch ( )
return request . cancel
} , [ ] )
}
# useCustomRequestCopy import { useCustomRequest } from '@cranium/resource' ;
function tryAsync ( API , payload , meta ) {
return new Promise ( ( resolve , reject ) => {
setTimeout ( ( ) => {
resolve ( { love : 'React' } )
} , 1000 )
} )
}
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data , request } = useCustomRequest ( tryAsync , resourceConfig )
}
# usePrefetchRequestCopy import { usePrefetchRequest } from '@cranium/resource' ;
function tryAsync ( API , payload , meta ) {
return new Promise ( ( resolve , reject ) => {
setTimeout ( ( ) => {
resolve ( { love : 'React' } )
} , 1000 )
} )
}
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const { data , request } = usePrefetchRequest ( tryAsync , resourceConfig )
}
# useRequestCopy import { useRequest } from '@cranium/resource' ;
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
function MyComponent ( ) {
const fetchUser = useRequest ( resourceConfig )
}
# prefetchResourcesCopy import { prefetchResources } from '@cranium/resource'
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
prefetchResources ( resourceConfig ) ( MyReactElement )
# customResourceCopy import { customResource , prefetchResources } from '@cranium/resource'
function myCustomFetch ( API , payload , meta ) {
return API . get ( '/context' )
}
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
const customConnectResource = customResource ( myCustomFetch )
prefetchResources ( customConnectResource ( resourceConfig ) ) ( MyReactElement )
# Several resourcesCopy import { prefetchResources } from '@cranium/resource'
const resourceConfig = {
namespace : 'profile' ,
endpoint : 'users/me' ,
cache : { storage : localStorage , key : 'profile' }
}
prefetchResources ( [
'users' ,
resourceConfig
] ) ( MyReactElement )