react-redux-cache 0.16.1-rc.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -3
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
|
|
10
10
|
# react-redux-cache (RRC)
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
### Supports both `Redux` and `Zustand`
|
|
13
|
+
|
|
14
|
+
**Powerful**, **performant** yet **lightweight** data fetching and caching library that supports **normalization** unlike `React Query` and `RTK-Query`, while having similar but not over-engineered, simple interface. Another advantage over `RTK-Query` is that it **doesn't use Immer** ([perf issue](https://github.com/reduxjs/redux-toolkit/issues/4793)). Covered with tests, fully typed and written on Typescript.
|
|
13
15
|
|
|
14
16
|
**Normalization** is the best way to keep the state of the app **consistent** between different views, reduces the number of fetches and allows to show cached data when navigating, which greatly improves **user experience**.
|
|
15
17
|
|
|
16
|
-
Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with **full control** over its storage - redux store, with ability to write custom selectors, actions and reducers to manage cached state.
|
|
18
|
+
Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with **full control** over its storage - redux or zustand store, with ability to write custom selectors, actions and reducers to manage cached state.
|
|
17
19
|
|
|
18
20
|
Examples of states, generated by cache reducer from `/example` project:
|
|
19
21
|
<details>
|
|
@@ -130,7 +132,7 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
130
132
|
`react` is a peer dependency.
|
|
131
133
|
|
|
132
134
|
`react-redux` and `fast-deep-equal` are optional peer dependencies:
|
|
133
|
-
- `react-redux` required when `storeHooks` is not provided when creating cache.
|
|
135
|
+
- `react-redux` required when `storeHooks` is not provided when creating cache. Not needed for Zustand.
|
|
134
136
|
- `fast-deep-equal` required if `deepComparisonEnabled` cache option is enabled (default is true).
|
|
135
137
|
|
|
136
138
|
```sh
|
|
@@ -143,10 +145,15 @@ npm add react-redux-cache react fast-deep-equal
|
|
|
143
145
|
# all required and optional peers
|
|
144
146
|
npm add react-redux-cache react react-redux fast-deep-equal
|
|
145
147
|
```
|
|
148
|
+
|
|
146
149
|
### Initialization
|
|
147
150
|
The only function that needs to be imported is either `withTypenames`, which is needed for normalization, or directly `createCache` if it is not needed. `createCache` creates fully typed reducer, hooks, actions, selectors and utils to be used in the app. You can create as many caches as needed, but keep in mind that normalization is not shared between them.
|
|
148
151
|
All queries and mutations should be passed while initializing the cache for proper typing.
|
|
152
|
+
|
|
149
153
|
#### cache.ts
|
|
154
|
+
|
|
155
|
+
> Zustand requires additional option - `storeHooks`.
|
|
156
|
+
|
|
150
157
|
```typescript
|
|
151
158
|
// Mapping of all typenames to their entity types, which is needed for proper normalization typing.
|
|
152
159
|
// Not needed if normalization is not used.
|
|
@@ -177,6 +184,10 @@ export const {
|
|
|
177
184
|
updateUser: { mutation: updateUser },
|
|
178
185
|
removeUser: { mutation: removeUser },
|
|
179
186
|
},
|
|
187
|
+
|
|
188
|
+
// Required for Zustand. Just an empty object can be passed during initialization, and hooks can be set later (see `store.ts` section).
|
|
189
|
+
// Can be also used for Redux if working with multiple stores.
|
|
190
|
+
storeHooks: {},
|
|
180
191
|
})
|
|
181
192
|
```
|
|
182
193
|
|
|
@@ -194,6 +205,8 @@ type EntityChanges<T extends Typenames> = {
|
|
|
194
205
|
```
|
|
195
206
|
|
|
196
207
|
#### store.ts
|
|
208
|
+
|
|
209
|
+
Redux:
|
|
197
210
|
```typescript
|
|
198
211
|
// Create store as usual, passing the new cache reducer under the name of the cache.
|
|
199
212
|
// If some other redux structure is needed, provide custom cacheStateSelector when creating cache.
|
|
@@ -204,6 +217,26 @@ const store = configureStore({
|
|
|
204
217
|
}
|
|
205
218
|
})
|
|
206
219
|
```
|
|
220
|
+
|
|
221
|
+
Zustand:
|
|
222
|
+
```typescript
|
|
223
|
+
type State = {[cache.name]: ReturnType<typeof reducer>}
|
|
224
|
+
type Actions = {dispatch: (action: CacheAction) => void}
|
|
225
|
+
type CacheAction = Parameters<typeof reducer>[1]
|
|
226
|
+
|
|
227
|
+
export const useStore = create<State & Actions>((set, get) => ({
|
|
228
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
229
|
+
[cache.name]: reducer(undefined, {} as any),
|
|
230
|
+
dispatch: (action: CacheAction) => {
|
|
231
|
+
set({cache: reducer(get().cache, action)})
|
|
232
|
+
},
|
|
233
|
+
}))
|
|
234
|
+
|
|
235
|
+
const store = {dispatch: useStore.getState().dispatch, getState: useStore.getState}
|
|
236
|
+
cache.storeHooks.useStore = () => store
|
|
237
|
+
cache.storeHooks.useSelector = useStore
|
|
238
|
+
```
|
|
239
|
+
|
|
207
240
|
#### api.ts
|
|
208
241
|
For normalization `normalizr` package is used in this example, but any other tool can be used if query result is of proper type.
|
|
209
242
|
Perfect implementation is when the backend already returns normalized data.
|
package/package.json
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
"name": "react-redux-cache",
|
|
3
3
|
"author": "Alexander Danilov",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.16.1
|
|
6
|
-
"description": "Powerful data fetching and caching library
|
|
5
|
+
"version": "0.16.1",
|
|
6
|
+
"description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
9
9
|
"scripts": {
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
"keywords": [
|
|
66
66
|
"react",
|
|
67
67
|
"redux",
|
|
68
|
+
"zustand",
|
|
68
69
|
"cache",
|
|
69
70
|
"query",
|
|
70
71
|
"normalization"
|