|
1 | 1 | # react-idb-cache |
2 | 2 |
|
3 | | -Use IndexedDB as a cache in React components. |
| 3 | +This library helps on one of the trickier parts of writing React components: |
| 4 | +How to render on ansynchronously fetched data and cache such data effectively. |
| 5 | + |
| 6 | +## Install |
| 7 | + |
| 8 | +``` |
| 9 | +$ yarn add react-idb-cache |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
4 | 13 |
|
5 | 14 | ```js |
6 | | -import { useIdbCache } from 'react-idb-cache' |
| 15 | +import { useCached } from 'react-idb-cache' |
7 | 16 |
|
8 | 17 | function MyComponent() { |
9 | | - const { get, set } = useIdbCache() |
| 18 | + const { get, set } = useCached() |
10 | 19 |
|
11 | 20 | // Get value from IndexedDB if it exists - the state will be updated if a value is received |
12 | 21 | const value = get('indexKeyForValue') |
13 | 22 |
|
14 | | - // Load a value from an API endpoint if it does not exist in the cache |
| 23 | + // Load a value from an API endpoint if it does not exist in the cache or IndexedDB |
15 | 24 | const otherValue = get('indexKeyForOtherValue', () => someRequest().then(({theRequiredValue, someOtherValue}) => { |
16 | 25 | // set the value in state and IndexedDB |
17 | 26 | set('indexKeyForOtherValue', theRequiredValue, {someMetaField: 123}) |
18 | | - |
19 | | - // also cache other received values to prevent unnecessary network round trips |
20 | | - set('indexKeyForSomeOtherValue', someOtherValue, {date: new Date()}) |
21 | 27 | })) |
22 | 28 |
|
23 | | - // Discard values (e.g. as expired) per callback |
24 | | - const validatedValue = get('indexKeyForValidatedValue', undefined, ({data, meta}) => meta.someMetaField > 123) |
| 29 | + // The component can render without blocking anything. |
| 30 | + // It will be updated once values are available. |
| 31 | + return <div> |
| 32 | + <div>{value}</div> |
| 33 | + <div>{otherValue}</div> |
| 34 | + </div> |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Setup |
| 39 | + |
| 40 | +```js |
| 41 | + const api = useCached({dbName: 'my-database', storeName: 'my-store'}) |
| 42 | +``` |
| 43 | +Per default the hook uses a store `keyval` in database `Cached`. |
| 44 | + |
| 45 | +### Get |
| 46 | + |
| 47 | +#### Get a single value |
| 48 | + |
| 49 | +```js |
| 50 | + const { get } = useCached() |
| 51 | + get('indexKeyForValue') |
| 52 | +``` |
| 53 | +#### Get multiple values |
| 54 | + |
| 55 | +```js |
| 56 | + const { get } = useCached() |
| 57 | + get(['keyOne', 'keyTwo']) |
| 58 | + |
| 59 | + // returns a record of key-value pairs |
| 60 | + { |
| 61 | + keyOne: 'foo', |
| 62 | + keyTwo: 'bar', |
| 63 | + } |
| 64 | +``` |
| 65 | + |
| 66 | +#### Load data from the backend |
| 67 | + |
| 68 | +```js |
| 69 | + const { get } = useCached() |
| 70 | + get(['keyOne', 'keyTwo'], myLoader) |
| 71 | + |
| 72 | + // if the cache/idb has a value for some of the keys, they are returned |
| 73 | + { |
| 74 | + keyOne: 'foo', |
| 75 | + keyTwo: undefined, |
| 76 | + } |
| 77 | +``` |
| 78 | + |
| 79 | +The `loader` is called - for multiple key requests with an array of missing keys - and should return a `Promise` that should resolve once the transaction is done. |
| 80 | + |
| 81 | +#### Reload outdated entries |
| 82 | + |
| 83 | +##### Callback |
| 84 | + |
| 85 | +You can discard entries from cache/idb per `expire` callback. |
| 86 | +If a `loader` is present, it will be called with the missing keys and those keys for which `expire` returned `true`. |
| 87 | + |
| 88 | +```js |
| 89 | + const { get } = useCached() |
| 90 | + get('someKey', myLoader, ({data, meta}) => meta.someMetaField > 123) |
| 91 | +``` |
| 92 | + |
| 93 | +##### Age |
25 | 94 |
|
26 | | - // Discard values if meta.date is set and older than 5 seconds |
27 | | - const expiringValue = get('indexKeyForExpiringValue', undefined, 5000) |
| 95 | +You can discard entries from cache/idb that are older than `expire` milliseconds. |
| 96 | +If a `loader` is present, it will be called with the missing keys and those keys for which `expire` returned `true`. |
28 | 97 |
|
29 | | - // ... |
| 98 | +```js |
| 99 | + const { get } = useCached() |
| 100 | + get('someKey', myLoader, 5000) // discard cached entry if meta.date is older that 5 seconds |
30 | 101 | } |
31 | 102 | ``` |
| 103 | + |
| 104 | +### Set |
| 105 | + |
| 106 | +#### Set a single value |
| 107 | + |
| 108 | +```js |
| 109 | + const { set } = useCached() |
| 110 | + set('someKey', 'someValue') |
| 111 | +``` |
| 112 | + |
| 113 | +The value can be anything that can be stored into IndexedDB. |
| 114 | + |
| 115 | +You can also store some meta data for the entry. |
| 116 | + |
| 117 | +```js |
| 118 | + const { set } = useCached() |
| 119 | + set('someKey', 'someValue', {someMetaKey: new Date()}) |
| 120 | +``` |
| 121 | + |
| 122 | +#### Set multiple values |
| 123 | + |
| 124 | +```js |
| 125 | + const { set } = useCached() |
| 126 | + set({ |
| 127 | + keyOne: 'someValue', |
| 128 | + keyTwo: 'otherValue', |
| 129 | + }, { |
| 130 | + keyOne: { |
| 131 | + someMetaKey: 'metaValue', |
| 132 | + }, |
| 133 | + }) |
| 134 | +``` |
| 135 | + |
| 136 | +#### Unset values |
| 137 | + |
| 138 | +You can also unset values in bulk action. |
| 139 | +```js |
| 140 | + const { set } = useCached() |
| 141 | + set({ |
| 142 | + keyOne: 'someValue', |
| 143 | + keyTwo: 'whatever', |
| 144 | + }, { |
| 145 | + keyTwo: null, // this will remove the entry for 'keyTwo' |
| 146 | + }) |
| 147 | +``` |
| 148 | + |
| 149 | +### Delete |
| 150 | + |
| 151 | +```js |
| 152 | + const { del } = useCached() |
| 153 | + del('someKey') |
| 154 | +``` |
| 155 | + |
| 156 | +### Clear |
| 157 | + |
| 158 | +#### Whole cache and idb-store |
| 159 | +```js |
| 160 | + const { clear } = useCached() |
| 161 | + clear() |
| 162 | +``` |
| 163 | + |
| 164 | +#### Filter entries per callback |
| 165 | +Clears the cache and also deletes some data from IndexedDB. |
| 166 | +```js |
| 167 | + const { clear } = useCached() |
| 168 | + clear(({data, meta}) => meta.someMetaField > 2) // this will remove all entries with meta.someMetaField > 2 |
| 169 | +``` |
0 commit comments