use-state-redux 1.0.0 → 1.0.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 +15 -15
- package/dist/index.js +1 -1
- package/dist/index.umd.cjs +1 -1
- package/dist/src/index.d.ts +2 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/{useReduxState.d.ts → usestateRedux.d.ts} +3 -2
- package/dist/src/{useReduxState.d.ts.map → usestateRedux.d.ts.map} +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# usestate-redux
|
|
2
2
|
|
|
3
3
|
A small React global state hook built on Redux Toolkit and Redux Persist. Use a
|
|
4
4
|
named state value from multiple components with an API similar to React's
|
|
5
5
|
`useState`:
|
|
6
6
|
|
|
7
7
|
```tsx
|
|
8
|
-
const [state, setState] =
|
|
8
|
+
const [state, setState] = usestateRedux("name", initialState);
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
The hook uses an internal Redux store, so you do not need to add a Redux
|
|
@@ -16,17 +16,17 @@ The hook uses an internal Redux store, so you do not need to add a Redux
|
|
|
16
16
|
Install this package and its peer dependencies:
|
|
17
17
|
|
|
18
18
|
```sh
|
|
19
|
-
npm install
|
|
19
|
+
npm install use-state-redux @reduxjs/toolkit react react-redux redux-persist
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
## Basic usage
|
|
24
24
|
|
|
25
25
|
```tsx
|
|
26
|
-
import {
|
|
26
|
+
import { usestateRedux } from "usestate-redux";
|
|
27
27
|
|
|
28
28
|
function Counter() {
|
|
29
|
-
const [count, setCount] =
|
|
29
|
+
const [count, setCount] = usestateRedux("count", 0);
|
|
30
30
|
|
|
31
31
|
return (
|
|
32
32
|
<button onClick={() => setCount((current) => current + 1)}>
|
|
@@ -41,12 +41,12 @@ same value:
|
|
|
41
41
|
|
|
42
42
|
```tsx
|
|
43
43
|
function CountLabel() {
|
|
44
|
-
const [count] =
|
|
44
|
+
const [count] = usestateRedux("count", 0);
|
|
45
45
|
return <p>{count}</p>;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
function ResetCountButton() {
|
|
49
|
-
const [, setCount] =
|
|
49
|
+
const [, setCount] = usestateRedux("count", 0);
|
|
50
50
|
return <button onClick={() => setCount(0)}>Reset count</button>;
|
|
51
51
|
}
|
|
52
52
|
```
|
|
@@ -69,7 +69,7 @@ State is not persisted by default. Set `persist: true` in the third argument to
|
|
|
69
69
|
save a named value with Redux Persist:
|
|
70
70
|
|
|
71
71
|
```tsx
|
|
72
|
-
const [theme, setTheme] =
|
|
72
|
+
const [theme, setTheme] = usestateRedux("theme", "light", {
|
|
73
73
|
persist: true,
|
|
74
74
|
});
|
|
75
75
|
```
|
|
@@ -87,12 +87,12 @@ enables persistence for that name for the lifetime of the app session. Keep the
|
|
|
87
87
|
|
|
88
88
|
## API
|
|
89
89
|
|
|
90
|
-
### `
|
|
90
|
+
### `usestateRedux(name, initialState, options?)`
|
|
91
91
|
|
|
92
92
|
Returns a tuple of the current value and setter:
|
|
93
93
|
|
|
94
94
|
```ts
|
|
95
|
-
const [state, setState] =
|
|
95
|
+
const [state, setState] = usestateRedux<T>(name, initialState, options?);
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
| Argument | Type | Description |
|
|
@@ -103,7 +103,7 @@ const [state, setState] = useReduxState<T>(name, initialState, options?);
|
|
|
103
103
|
|
|
104
104
|
### `useRedux(name, initialState, options?)`
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
The `useRedux` export is an alias for `usestateRedux`.
|
|
107
107
|
|
|
108
108
|
### `setGlobalStateValue(name, value)`
|
|
109
109
|
|
|
@@ -111,7 +111,7 @@ Set a named value outside a component. The matching hook updates when the Redux
|
|
|
111
111
|
store changes.
|
|
112
112
|
|
|
113
113
|
```ts
|
|
114
|
-
import { setGlobalStateValue } from "
|
|
114
|
+
import { setGlobalStateValue } from "usestate-redux";
|
|
115
115
|
|
|
116
116
|
setGlobalStateValue("count", 10);
|
|
117
117
|
```
|
|
@@ -121,7 +121,7 @@ setGlobalStateValue("count", 10);
|
|
|
121
121
|
Restore a registered name to the initial value from its first hook call.
|
|
122
122
|
|
|
123
123
|
```ts
|
|
124
|
-
import { resetGlobalState } from "
|
|
124
|
+
import { resetGlobalState } from "usestate-redux";
|
|
125
125
|
|
|
126
126
|
resetGlobalState("count");
|
|
127
127
|
```
|
|
@@ -137,7 +137,7 @@ need direct access to the internal Redux store or persistor. For example, the
|
|
|
137
137
|
persistor can be used to flush pending writes or purge persisted data:
|
|
138
138
|
|
|
139
139
|
```ts
|
|
140
|
-
import { reduxPersistor } from "
|
|
140
|
+
import { reduxPersistor } from "usestate-redux";
|
|
141
141
|
|
|
142
142
|
await reduxPersistor.flush();
|
|
143
143
|
// To remove the persisted store instead:
|
|
@@ -146,7 +146,7 @@ await reduxPersistor.flush();
|
|
|
146
146
|
|
|
147
147
|
## Exports
|
|
148
148
|
|
|
149
|
-
- `
|
|
149
|
+
- `usestateRedux` and `useRedux`
|
|
150
150
|
- `setGlobalStateValue`
|
|
151
151
|
- `resetGlobalState` and `resetAllGlobalState`
|
|
152
152
|
- `reduxStore` and `reduxPersistor`
|
package/dist/index.js
CHANGED
package/dist/index.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(o,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react"),require("@reduxjs/toolkit"),require("redux-persist")):typeof define=="function"&&define.amd?define(["exports","react","@reduxjs/toolkit","redux-persist"],n):(o=typeof globalThis<"u"?globalThis:o||self,n(o.
|
|
1
|
+
(function(o,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react"),require("@reduxjs/toolkit"),require("redux-persist")):typeof define=="function"&&define.amd?define(["exports","react","@reduxjs/toolkit","redux-persist"],n):(o=typeof globalThis<"u"?globalThis:o||self,n(o.UsestateRedux={},o.React,o.ReduxToolkit,o.ReduxPersist))})(this,function(o,n,h,u){"use strict";var p={},g={};g.__esModule=!0,g.default=V;function S(e){return typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?S=function(r){return typeof r}:S=function(r){return r&&typeof Symbol=="function"&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},S(e)}function b(){}var E={getItem:b,setItem:b,removeItem:b};function w(e){if((typeof self>"u"?"undefined":S(self))!=="object"||!(e in self))return!1;try{var t=self[e],r="redux-persist ".concat(e," test");t.setItem(r,"test"),t.getItem(r),t.removeItem(r)}catch{return process.env.NODE_ENV!=="production"&&console.warn("redux-persist ".concat(e," test failed, persistence will be disabled.")),!1}return!0}function V(e){var t="".concat(e,"Storage");return w(t)?self[t]:(process.env.NODE_ENV!=="production"&&console.error("redux-persist failed to create sync storage. falling back to noop storage."),E)}p.__esModule=!0,p.default=N;var k=M(g);function M(e){return e&&e.__esModule?e:{default:e}}function N(e){var t=(0,k.default)(e);return{getItem:function(s){return new Promise(function(l,d){l(t.getItem(s))})},setItem:function(s,l){return new Promise(function(d,G){d(t.setItem(s,l))})},removeItem:function(s){return new Promise(function(l,d){l(t.removeItem(s))})}}}var m=void 0,T=q(p);function q(e){return e&&e.__esModule?e:{default:e}}var x=(0,T.default)("local");m=x;const _=h.createSlice({name:"globalState",initialState:{},reducers:{setGlobalStateValue(e,t){e[t.payload.name]=t.payload.value},removeGlobalStateValue(e,t){delete e[t.payload]},resetGlobalStateValues(){return{}}}}),{setGlobalStateValue:y,removeGlobalStateValue:A,resetGlobalStateValues:D}=_.actions,O=_.reducer,R=[],v=new Set,f=new Set,P=u.persistReducer({key:"useredux",storage:m,whitelist:R},O),a=h.configureStore({reducer:P,middleware:e=>e({serializableCheck:{ignoredActions:[u.FLUSH,u.REHYDRATE,u.PAUSE,u.PERSIST,u.PURGE,u.REGISTER],ignoredPaths:["_persist"]}})}),i=new Map,C=u.persistStore(a);function U(e,t,r){i.has(e)||i.set(e,t),r.persist&&!v.has(e)&&(v.add(e),R.push(e))}function I(e,t,r={}){U(e,t,r);const s=n.useCallback(()=>{const c=a.getState();return Object.prototype.hasOwnProperty.call(c,e)&&(v.has(e)||f.has(e))?c[e]:i.get(e)},[e]),l=n.useCallback(()=>i.get(e),[e]),d=n.useSyncExternalStore(a.subscribe,s,l),G=n.useCallback(c=>{const z=s(),F=typeof c=="function"?c(z):c;f.add(e),a.dispatch(y({name:e,value:F}))},[s,e]);return[d,G]}function $(e,t){f.add(e),a.dispatch(y({name:e,value:t}))}function W(e){i.has(e)?(f.add(e),a.dispatch(y({name:e,value:i.get(e)}))):a.dispatch(A(e))}function H(){f.clear(),a.dispatch(D())}o.reduxPersistor=C,o.reduxStore=a,o.resetAllGlobalState=H,o.resetGlobalState=W,o.setGlobalStateValue=$,o.useRedux=I,o.usestateRedux=I,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export { resetAllGlobalState, resetGlobalState, reduxStore, reduxPersistor, setGlobalStateValue, useRedux, } from './
|
|
2
|
-
export type { GlobalStateSetter, UseReduxOptions } from './
|
|
3
|
-
export { useRedux as useReduxState } from './useReduxState.js';
|
|
1
|
+
export { resetAllGlobalState, resetGlobalState, reduxStore, reduxPersistor, setGlobalStateValue, usestateRedux, useRedux, } from './usestateRedux.js';
|
|
2
|
+
export type { GlobalStateSetter, UseReduxOptions } from './usestateRedux.js';
|
|
4
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,mBAAmB,EACnB,QAAQ,GACT,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,EACV,cAAc,EACd,mBAAmB,EACnB,aAAa,EACb,QAAQ,GACT,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -16,7 +16,7 @@ export type GlobalStateSetter<T> = Dispatch<SetStateAction<T>>;
|
|
|
16
16
|
* name share the same value and ignore their own initial value. Persistence is
|
|
17
17
|
* enabled for a name when any caller opts in; it stays enabled for that name.
|
|
18
18
|
*/
|
|
19
|
-
export declare function
|
|
19
|
+
export declare function usestateRedux<T>(name: string, initialState: T, options?: UseReduxOptions): [T, GlobalStateSetter<T>];
|
|
20
20
|
/** Set a named global value outside a React component. */
|
|
21
21
|
export declare function setGlobalStateValue<T>(name: string, value: T): void;
|
|
22
22
|
/** Restore a named value to the initial value supplied to its first hook call. */
|
|
@@ -24,4 +24,5 @@ export declare function resetGlobalState(name: string): void;
|
|
|
24
24
|
/** Reset every named value to its initial value. */
|
|
25
25
|
export declare function resetAllGlobalState(): void;
|
|
26
26
|
export { store as reduxStore };
|
|
27
|
-
|
|
27
|
+
export { usestateRedux as useRedux };
|
|
28
|
+
//# sourceMappingURL=usestateRedux.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"
|
|
1
|
+
{"version":3,"file":"usestateRedux.d.ts","sourceRoot":"","sources":["../../src/usestateRedux.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAatD,OAA2B,EAIzB,KAAK,WAAW,EACjB,MAAM,YAAY,CAAC;AAiBpB,QAAA,MAAM,KAAK;;oCAST,CAAC;AAGH,eAAO,MAAM,cAAc,mCAAsB,CAAC;AAElD,MAAM,WAAW,eAAe;IAC9B,mEAAmE;IACnE,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAiBD,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,QAAQ,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;AAE/D;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAC7B,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,CAAC,EACf,OAAO,GAAE,eAAoB,GAC5B,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAuC3B;AAED,0DAA0D;AAC1D,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAGnE;AAED,kFAAkF;AAClF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CASnD;AAED,oDAAoD;AACpD,wBAAgB,mBAAmB,IAAI,IAAI,CAG1C;AAED,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,CAAC;AAG/B,OAAO,EAAE,aAAa,IAAI,QAAQ,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "use-state-redux",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A zero-boilerplate Redux wrapper providing a useState-like global hook experience",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.umd.cjs",
|
|
@@ -36,4 +36,4 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@types/redux-persist": "^4.0.0"
|
|
38
38
|
}
|
|
39
|
-
}
|
|
39
|
+
}
|