@quanta-lib/q-state 1.1.0 → 1.1.2
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/package.json +1 -1
- package/src/Q-State-Engine.ts +14 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quanta-lib/q-state",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "A lightweight React state management library using `useSyncExternalStore` with optional localStorage caching and value transformation.",
|
|
5
5
|
"main": "dist/Q-State-Engine.js",
|
|
6
6
|
"types": "src/Q-State-Engine.ts",
|
package/src/Q-State-Engine.ts
CHANGED
|
@@ -38,7 +38,7 @@ export class Q_StateEngine
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
useQuantaState = <K extends keyof T>(key: K): [T[K]] => {
|
|
41
|
-
const
|
|
41
|
+
const getCurrAfterRender = () => {
|
|
42
42
|
if (this.option?.cache && typeof window !== "undefined") {
|
|
43
43
|
const getcache = localStorage.getItem(key as string)
|
|
44
44
|
if (getcache !== null && getcache !== 'undefined') {
|
|
@@ -55,11 +55,21 @@ useQuantaState = <K extends keyof T>(key: K): [T[K]] => {
|
|
|
55
55
|
}
|
|
56
56
|
return this.obj[key]
|
|
57
57
|
}
|
|
58
|
+
let isHydrated = false
|
|
59
|
+
const defaultValue = null
|
|
58
60
|
const syncSpecificData = useSyncExternalStore(
|
|
59
61
|
this.subsribe,
|
|
60
|
-
|
|
61
|
-
|
|
62
|
+
() => {
|
|
63
|
+
if (!isHydrated) {
|
|
64
|
+
if (typeof window !== 'undefined') {
|
|
65
|
+
setTimeout(() => { isHydrated = true }, 0);
|
|
66
|
+
}
|
|
67
|
+
return defaultValue
|
|
68
|
+
}
|
|
69
|
+
return getCurrAfterRender()
|
|
70
|
+
},
|
|
71
|
+
() => defaultValue
|
|
62
72
|
)
|
|
63
|
-
return [syncSpecificData]
|
|
73
|
+
return [syncSpecificData] as [T[K]]
|
|
64
74
|
}
|
|
65
75
|
}
|