redux-astroglide 0.1.12 → 0.1.13

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 CHANGED
@@ -1,8 +1,8 @@
1
1
  # Redux-Astroglide
2
2
 
3
- #### Taking the pain out of state management by stuffing a huge package into a tiny API
3
+ #### Redux doesn't need to be such a pain the butt!
4
4
 
5
- Astroglide is a set of configuration and automation tools built on top of Redux Toolkit in order to provide the most succinct API with the least boilerplate possible. It's the easiest way to get up and running with redux state, and has the lowest mental overhead of any state management tool for React.
5
+ Redux-Astroglide is a set of configuration and automation tools built on top of Redux Toolkit in order to provide the most succinct API with the least boilerplate possible. It's the easiest way to get up and running with redux state, and has the lowest mental overhead of any state management tool for React.
6
6
 
7
7
  We stay DRY so you don't have to.
8
8
 
@@ -13,10 +13,14 @@ We stay DRY so you don't have to.
13
13
  ```bash
14
14
  # NPM
15
15
  npm install @reduxjs/toolkit redux-astroglide
16
+ ```
16
17
 
18
+ ```bash
17
19
  # Yarn
18
20
  yarn add @reduxjs/toolkit redux-astroglide
21
+ ```
19
22
 
23
+ ```bash
20
24
  # PNPM
21
25
  pnpm add @reduxjs/toolkit redux-astroglide
22
26
 
@@ -54,10 +58,11 @@ const slice = createSlice(
54
58
  // initial state
55
59
  username: "",
56
60
  password: "",
61
+ count: 0,
57
62
  }
58
63
  );
59
64
 
60
- export const { setPassword, setUsername } = slice.actions;
65
+ export const { setUsername, setPassword } = slice.actions;
61
66
  export const { selectUsername, selectPassword } = slice.selectors;
62
67
  export const { useUsername, usePassword } = slice.hooks;
63
68
  ```
@@ -72,6 +77,7 @@ const slice = createSlice({
72
77
  initialState: {
73
78
  username: "",
74
79
  password: "",
80
+ count: 0,
75
81
  },
76
82
  reducers: {
77
83
  // custom reducers are the most likely reason to use this syntax
@@ -109,12 +115,25 @@ export const UsernameField = (props) => {
109
115
  name="username"
110
116
  type="text"
111
117
  value={username}
112
- onChange={(e) => setUsername(e.target.value)} // this triggers a redux action
118
+ onChange={
119
+ (e) => setUsername(e.target.value) // dispatch a redux action named "LoginForm/setUsername"
120
+ }
113
121
  />
114
122
  );
115
123
  };
116
124
  ```
117
125
 
126
+ You can pass a function to the hook to assign a custom value setter for that instance of the hook:
127
+
128
+ ```jsx
129
+ // component.js
130
+ function Component() {
131
+ const [count, increment] = useCount((currentCount) => currentCount + 1);
132
+
133
+ // ...
134
+ }
135
+ ```
136
+
118
137
   
119
138
 
120
139
  The setter actions can be passed a function to receive a copy of the latest state value, just like with React's setState:
@@ -209,7 +228,7 @@ function* loginSaga(action) {
209
228
 
210
229
   
211
230
 
212
- Astroglide also provides some of its internal helper functions you may find useful:
231
+ Astroglide also provides some of its internal helper functions:
213
232
 
214
233
  ```jsx
215
234
  const configure = "redux-astroglide";
@@ -217,6 +236,7 @@ const configure = "redux-astroglide";
217
236
  export const {
218
237
  store,
219
238
  createSlice,
239
+
220
240
  injectReducer, // injectReducer(key: string, state => state: reducer fn, optionally async)
221
241
  injectSlice, // injectSlice(slice: result from createSlice())
222
242
  injectMiddleware, // injectMiddleware(middleware: redux middleware)
@@ -227,7 +247,7 @@ export const {
227
247
 
228
248
  ## Plugins
229
249
 
230
- Astroglide ships with a handful of plugins you can use for things like typechecking and data persistence:
250
+ Astroglide comes with plugins you can use for typechecking, data persistence and custom value and state setters:
231
251
 
232
252
  ```jsx
233
253
  // Login/slice.js
@@ -238,10 +258,14 @@ const slice = createSlice("Login", {
238
258
 
239
259
  // Nav/slice.js
240
260
  const slice = createSlice("Nav", {
241
- isOpen: persist("", {
242
- storageType: localStorage, // default localStorage, must provide { getItem(key):any, setItem(key, value):void } API (async not allowed)
261
+ token: persist("", {
262
+ storageType: localStorage, // default localStorage, or API passed to setup fn, must provide { getItem(key):any, setItem(key, value):void } API (async not allowed)
243
263
  }),
244
264
  clickCount: set((value) => value + 1),
265
+ instanceCount: set((value, { draft }) => {
266
+ draft.clickCount = 0;
267
+ return value + 1;
268
+ }),
245
269
  });
246
270
  ```
247
271
 
@@ -255,13 +279,18 @@ import configure, { addPlugins } from "redux-astroglide";
255
279
  import setPlugin from "redux-astroglide/plugins/set";
256
280
  import typePlugin from "redux-astroglide/plugins/type";
257
281
  import persistPlugin from "redux-astroglide/plugins/persist";
258
- // these can also be imported collectively like:
282
+ // these can also be imported like:
259
283
  // import { set, type, persist } = "redux-astroglide/plugins";
260
284
 
261
285
  export const [set, type, persist] = addPlugins(
262
286
  setPlugin(),
263
287
  typePlugin({ shouldPreventUpdate: false }),
264
- persistPlugin()
288
+ persistPlugin({
289
+ storageType: { // defaults to localStorage (async storage not allowed)
290
+ getItem(key: string),
291
+ setItem(key: string, value: string)}
292
+ }
293
+ )
265
294
  );
266
295
 
267
296
  export const { store, createSlice } = configure();
@@ -278,7 +307,8 @@ import {
278
307
  const currentValue = getPersistedValue(
279
308
  "isOpen",
280
309
  "Nav"
281
- // storageType: localStorage | sessionStorage | { getItem, setItem }
310
+ // storageType: localStorage | sessionStorage |
311
+ // { getItem(key: string), setItem(key: string, value:string) }
282
312
  );
283
313
 
284
314
  storePersistedValue(