hank-zilla 1.2.1 â 1.2.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/LICENSE +21 -21
- package/README.md +209 -209
- package/dist/LICENSE +21 -21
- package/dist/README.md +209 -209
- package/dist/useAppState/appState.js +1 -1
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 phamdung2209
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 phamdung2209
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,209 +1,209 @@
|
|
|
1
|
-
# ðĶ Hank-Zilla
|
|
2
|
-
|
|
3
|
-
A lightweight React state management library that eliminates the need for Context, Redux, or Providers. Share state across components as easily as `useState` with global accessibility.
|
|
4
|
-
|
|
5
|
-
[](https://badge.fury.io/js/hank-zilla)
|
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[](https://www.typescriptlang.org/)
|
|
8
|
-
|
|
9
|
-
## âĻ Features
|
|
10
|
-
|
|
11
|
-
- ð **No Provider Required**: Share state globally without wrapping components in Providers
|
|
12
|
-
- ð **Drop-in Replacement**: Replace `useState` with `useAppState` for global state
|
|
13
|
-
- ðŊ **Access Anywhere**: Use the same state in any component, anywhere in your app
|
|
14
|
-
- ðŠķ **Lightweight**: Zero dependencies, minimal bundle size (~2.1KB)
|
|
15
|
-
- ⥠**Fast**: Built on React's `useSyncExternalStore` for optimal performance
|
|
16
|
-
- ðĶ **TypeScript**: Full TypeScript support with type inference
|
|
17
|
-
|
|
18
|
-
## ðĶ Installation
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install hank-zilla
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## ð Quick Start
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
import { useAppState, setAppState, getAppState } from 'hank-zilla'
|
|
28
|
-
|
|
29
|
-
// Parent Component - Set state
|
|
30
|
-
function ParentComponent() {
|
|
31
|
-
const user = useAppState('user', { name: '', email: '' })
|
|
32
|
-
|
|
33
|
-
return (
|
|
34
|
-
<div>
|
|
35
|
-
<button
|
|
36
|
-
onClick={() => setAppState('user', { name: 'John Doe', email: 'john@example.com' })}
|
|
37
|
-
>
|
|
38
|
-
Set User
|
|
39
|
-
</button>
|
|
40
|
-
<ChildComponent />
|
|
41
|
-
</div>
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Child Component - Access same state (no props needed!)
|
|
46
|
-
function ChildComponent() {
|
|
47
|
-
const user = useAppState('user')
|
|
48
|
-
|
|
49
|
-
return <div>{user ? `Welcome, ${user.name}!` : 'No user logged in'}</div>
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## ð API Reference
|
|
54
|
-
|
|
55
|
-
### `useAppState(key, defaultValue?, removeOnUnmount?)`
|
|
56
|
-
|
|
57
|
-
React hook for managing global state - works exactly like `useState` but globally accessible.
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
// Basic usage
|
|
61
|
-
const count = useAppState('count', 0)
|
|
62
|
-
|
|
63
|
-
// Auto-cleanup when component unmounts
|
|
64
|
-
const tempData = useAppState('tempData', null, true)
|
|
65
|
-
|
|
66
|
-
// TypeScript support
|
|
67
|
-
interface User {
|
|
68
|
-
name: string
|
|
69
|
-
email: string
|
|
70
|
-
}
|
|
71
|
-
const user = useAppState<User>('user', null)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### `setAppState(key, value)`
|
|
75
|
-
|
|
76
|
-
Update state from anywhere in your app - works with values or updater functions.
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
// Set value directly
|
|
80
|
-
setAppState('count', 42)
|
|
81
|
-
|
|
82
|
-
// Update with function (like useState)
|
|
83
|
-
setAppState('count', (prev) => prev + 1)
|
|
84
|
-
|
|
85
|
-
// Update object (shallow merge)
|
|
86
|
-
setAppState('user', { name: 'John' })
|
|
87
|
-
|
|
88
|
-
// Update array
|
|
89
|
-
setAppState('items', (prev) => [...prev, 'new item'])
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### `getAppState(key?)`
|
|
93
|
-
|
|
94
|
-
Get state value without subscribing to changes (no re-renders).
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
// Get specific key
|
|
98
|
-
const currentCount = getAppState('count')
|
|
99
|
-
|
|
100
|
-
// Get all state
|
|
101
|
-
const allState = getAppState()
|
|
102
|
-
|
|
103
|
-
// â ïļ Note: This doesn't trigger re-renders when state changes
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### `$getAppState` (Console Helper)
|
|
107
|
-
|
|
108
|
-
Debug helper available in browser console to inspect your app state.
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
// In browser console
|
|
112
|
-
$getAppState // Returns all state
|
|
113
|
-
$getAppState('key') // Returns specific key
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## ðŊ Advanced Usage
|
|
117
|
-
|
|
118
|
-
### Auto-cleanup with `removeOnUnmount`
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
function TemporaryComponent() {
|
|
122
|
-
// This state will be deleted when component unmounts
|
|
123
|
-
const [data, setData] = useAppState('tempData', [], true)
|
|
124
|
-
|
|
125
|
-
return <div>Temporary data: {data.length} items</div>
|
|
126
|
-
}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### TypeScript Support
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
interface CartItem {
|
|
133
|
-
id: string
|
|
134
|
-
name: string
|
|
135
|
-
price: number
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Type inference
|
|
139
|
-
const [cart, setCart] = useAppState<CartItem[]>('cart', [])
|
|
140
|
-
|
|
141
|
-
// Add item with type safety
|
|
142
|
-
const addItem = (item: CartItem) => {
|
|
143
|
-
setCart((prev) => [...prev, item])
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Performance: When to use `getAppState`
|
|
148
|
-
|
|
149
|
-
```typescript
|
|
150
|
-
// â
Good: Use useAppState for UI components
|
|
151
|
-
function Counter() {
|
|
152
|
-
const [count, setCount] = useAppState('count', 0)
|
|
153
|
-
return <div>Count: {count}</div> // Re-renders when count changes
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// â
Good: Use getAppState for utilities/callbacks
|
|
157
|
-
function logCurrentState() {
|
|
158
|
-
const count = getAppState('count')
|
|
159
|
-
console.log('Current count:', count) // No re-renders
|
|
160
|
-
}
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
## ð Comparison
|
|
164
|
-
|
|
165
|
-
| Feature | Hank-Zilla | Context API | Redux | Zustand |
|
|
166
|
-
| ----------------- | ----------- | ----------------- | ------------------ | ----------- |
|
|
167
|
-
| **Setup** | â None | â
Provider | â
Store + Actions | â
Store |
|
|
168
|
-
| **Bundle Size** | ðĒ ~1.3KB | ðĒ Built-in | ðī ~45KB | ðĄ ~8KB |
|
|
169
|
-
| **Global Access** | â
Anywhere | â Provider scope | â
Anywhere | â
Anywhere |
|
|
170
|
-
| **TypeScript** | ðĒ Full | ðĄ Manual | ðĒ Full | ðĒ Full |
|
|
171
|
-
|
|
172
|
-
## ð§ Migration
|
|
173
|
-
|
|
174
|
-
### From `useState` to `useAppState`
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
// Before: Local state
|
|
178
|
-
const [count, setCount] = useState(0)
|
|
179
|
-
|
|
180
|
-
// After: Global state
|
|
181
|
-
const count = useAppState('count', 0)
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### From Context API
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
// Before: Complex setup
|
|
188
|
-
const UserContext = createContext()
|
|
189
|
-
const { user, setUser } = useContext(UserContext)
|
|
190
|
-
|
|
191
|
-
// After: Direct usage
|
|
192
|
-
const user = useAppState('user', null)
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## ðĪ Contributing
|
|
196
|
-
|
|
197
|
-
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
198
|
-
|
|
199
|
-
## ð License
|
|
200
|
-
|
|
201
|
-
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
202
|
-
|
|
203
|
-
## ð Acknowledgments
|
|
204
|
-
|
|
205
|
-
Built with âĪïļ by [phamdung2209](https://github.com/phamdung2209) âĒ Special thanks to the React team for `useSyncExternalStore`
|
|
206
|
-
|
|
207
|
-
---
|
|
208
|
-
|
|
209
|
-
Made with ðĶ by the Hank-Zilla team
|
|
1
|
+
# ðĶ Hank-Zilla
|
|
2
|
+
|
|
3
|
+
A lightweight React state management library that eliminates the need for Context, Redux, or Providers. Share state across components as easily as `useState` with global accessibility.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/hank-zilla)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
## âĻ Features
|
|
10
|
+
|
|
11
|
+
- ð **No Provider Required**: Share state globally without wrapping components in Providers
|
|
12
|
+
- ð **Drop-in Replacement**: Replace `useState` with `useAppState` for global state
|
|
13
|
+
- ðŊ **Access Anywhere**: Use the same state in any component, anywhere in your app
|
|
14
|
+
- ðŠķ **Lightweight**: Zero dependencies, minimal bundle size (~2.1KB)
|
|
15
|
+
- ⥠**Fast**: Built on React's `useSyncExternalStore` for optimal performance
|
|
16
|
+
- ðĶ **TypeScript**: Full TypeScript support with type inference
|
|
17
|
+
|
|
18
|
+
## ðĶ Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install hank-zilla
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## ð Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { useAppState, setAppState, getAppState } from 'hank-zilla'
|
|
28
|
+
|
|
29
|
+
// Parent Component - Set state
|
|
30
|
+
function ParentComponent() {
|
|
31
|
+
const user = useAppState('user', { name: '', email: '' })
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div>
|
|
35
|
+
<button
|
|
36
|
+
onClick={() => setAppState('user', { name: 'John Doe', email: 'john@example.com' })}
|
|
37
|
+
>
|
|
38
|
+
Set User
|
|
39
|
+
</button>
|
|
40
|
+
<ChildComponent />
|
|
41
|
+
</div>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Child Component - Access same state (no props needed!)
|
|
46
|
+
function ChildComponent() {
|
|
47
|
+
const user = useAppState('user')
|
|
48
|
+
|
|
49
|
+
return <div>{user ? `Welcome, ${user.name}!` : 'No user logged in'}</div>
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## ð API Reference
|
|
54
|
+
|
|
55
|
+
### `useAppState(key, defaultValue?, removeOnUnmount?)`
|
|
56
|
+
|
|
57
|
+
React hook for managing global state - works exactly like `useState` but globally accessible.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Basic usage
|
|
61
|
+
const count = useAppState('count', 0)
|
|
62
|
+
|
|
63
|
+
// Auto-cleanup when component unmounts
|
|
64
|
+
const tempData = useAppState('tempData', null, true)
|
|
65
|
+
|
|
66
|
+
// TypeScript support
|
|
67
|
+
interface User {
|
|
68
|
+
name: string
|
|
69
|
+
email: string
|
|
70
|
+
}
|
|
71
|
+
const user = useAppState<User>('user', null)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `setAppState(key, value)`
|
|
75
|
+
|
|
76
|
+
Update state from anywhere in your app - works with values or updater functions.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// Set value directly
|
|
80
|
+
setAppState('count', 42)
|
|
81
|
+
|
|
82
|
+
// Update with function (like useState)
|
|
83
|
+
setAppState('count', (prev) => prev + 1)
|
|
84
|
+
|
|
85
|
+
// Update object (shallow merge)
|
|
86
|
+
setAppState('user', { name: 'John' })
|
|
87
|
+
|
|
88
|
+
// Update array
|
|
89
|
+
setAppState('items', (prev) => [...prev, 'new item'])
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `getAppState(key?)`
|
|
93
|
+
|
|
94
|
+
Get state value without subscribing to changes (no re-renders).
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Get specific key
|
|
98
|
+
const currentCount = getAppState('count')
|
|
99
|
+
|
|
100
|
+
// Get all state
|
|
101
|
+
const allState = getAppState()
|
|
102
|
+
|
|
103
|
+
// â ïļ Note: This doesn't trigger re-renders when state changes
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### `$getAppState` (Console Helper)
|
|
107
|
+
|
|
108
|
+
Debug helper available in browser console to inspect your app state.
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
// In browser console
|
|
112
|
+
$getAppState // Returns all state
|
|
113
|
+
$getAppState('key') // Returns specific key
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## ðŊ Advanced Usage
|
|
117
|
+
|
|
118
|
+
### Auto-cleanup with `removeOnUnmount`
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
function TemporaryComponent() {
|
|
122
|
+
// This state will be deleted when component unmounts
|
|
123
|
+
const [data, setData] = useAppState('tempData', [], true)
|
|
124
|
+
|
|
125
|
+
return <div>Temporary data: {data.length} items</div>
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### TypeScript Support
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
interface CartItem {
|
|
133
|
+
id: string
|
|
134
|
+
name: string
|
|
135
|
+
price: number
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Type inference
|
|
139
|
+
const [cart, setCart] = useAppState<CartItem[]>('cart', [])
|
|
140
|
+
|
|
141
|
+
// Add item with type safety
|
|
142
|
+
const addItem = (item: CartItem) => {
|
|
143
|
+
setCart((prev) => [...prev, item])
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Performance: When to use `getAppState`
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// â
Good: Use useAppState for UI components
|
|
151
|
+
function Counter() {
|
|
152
|
+
const [count, setCount] = useAppState('count', 0)
|
|
153
|
+
return <div>Count: {count}</div> // Re-renders when count changes
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// â
Good: Use getAppState for utilities/callbacks
|
|
157
|
+
function logCurrentState() {
|
|
158
|
+
const count = getAppState('count')
|
|
159
|
+
console.log('Current count:', count) // No re-renders
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## ð Comparison
|
|
164
|
+
|
|
165
|
+
| Feature | Hank-Zilla | Context API | Redux | Zustand |
|
|
166
|
+
| ----------------- | ----------- | ----------------- | ------------------ | ----------- |
|
|
167
|
+
| **Setup** | â None | â
Provider | â
Store + Actions | â
Store |
|
|
168
|
+
| **Bundle Size** | ðĒ ~1.3KB | ðĒ Built-in | ðī ~45KB | ðĄ ~8KB |
|
|
169
|
+
| **Global Access** | â
Anywhere | â Provider scope | â
Anywhere | â
Anywhere |
|
|
170
|
+
| **TypeScript** | ðĒ Full | ðĄ Manual | ðĒ Full | ðĒ Full |
|
|
171
|
+
|
|
172
|
+
## ð§ Migration
|
|
173
|
+
|
|
174
|
+
### From `useState` to `useAppState`
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// Before: Local state
|
|
178
|
+
const [count, setCount] = useState(0)
|
|
179
|
+
|
|
180
|
+
// After: Global state
|
|
181
|
+
const count = useAppState('count', 0)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### From Context API
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
// Before: Complex setup
|
|
188
|
+
const UserContext = createContext()
|
|
189
|
+
const { user, setUser } = useContext(UserContext)
|
|
190
|
+
|
|
191
|
+
// After: Direct usage
|
|
192
|
+
const user = useAppState('user', null)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## ðĪ Contributing
|
|
196
|
+
|
|
197
|
+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
198
|
+
|
|
199
|
+
## ð License
|
|
200
|
+
|
|
201
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
202
|
+
|
|
203
|
+
## ð Acknowledgments
|
|
204
|
+
|
|
205
|
+
Built with âĪïļ by [phamdung2209](https://github.com/phamdung2209) âĒ Special thanks to the React team for `useSyncExternalStore`
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
Made with ðĶ by the Hank-Zilla team
|
package/dist/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 phamdung2209
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 phamdung2209
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/README.md
CHANGED
|
@@ -1,209 +1,209 @@
|
|
|
1
|
-
# ðĶ Hank-Zilla
|
|
2
|
-
|
|
3
|
-
A lightweight React state management library that eliminates the need for Context, Redux, or Providers. Share state across components as easily as `useState` with global accessibility.
|
|
4
|
-
|
|
5
|
-
[](https://badge.fury.io/js/hank-zilla)
|
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[](https://www.typescriptlang.org/)
|
|
8
|
-
|
|
9
|
-
## âĻ Features
|
|
10
|
-
|
|
11
|
-
- ð **No Provider Required**: Share state globally without wrapping components in Providers
|
|
12
|
-
- ð **Drop-in Replacement**: Replace `useState` with `useAppState` for global state
|
|
13
|
-
- ðŊ **Access Anywhere**: Use the same state in any component, anywhere in your app
|
|
14
|
-
- ðŠķ **Lightweight**: Zero dependencies, minimal bundle size (~2.1KB)
|
|
15
|
-
- ⥠**Fast**: Built on React's `useSyncExternalStore` for optimal performance
|
|
16
|
-
- ðĶ **TypeScript**: Full TypeScript support with type inference
|
|
17
|
-
|
|
18
|
-
## ðĶ Installation
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install hank-zilla
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## ð Quick Start
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
import { useAppState, setAppState, getAppState } from 'hank-zilla'
|
|
28
|
-
|
|
29
|
-
// Parent Component - Set state
|
|
30
|
-
function ParentComponent() {
|
|
31
|
-
const user = useAppState('user', { name: '', email: '' })
|
|
32
|
-
|
|
33
|
-
return (
|
|
34
|
-
<div>
|
|
35
|
-
<button
|
|
36
|
-
onClick={() => setAppState('user', { name: 'John Doe', email: 'john@example.com' })}
|
|
37
|
-
>
|
|
38
|
-
Set User
|
|
39
|
-
</button>
|
|
40
|
-
<ChildComponent />
|
|
41
|
-
</div>
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Child Component - Access same state (no props needed!)
|
|
46
|
-
function ChildComponent() {
|
|
47
|
-
const user = useAppState('user')
|
|
48
|
-
|
|
49
|
-
return <div>{user ? `Welcome, ${user.name}!` : 'No user logged in'}</div>
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## ð API Reference
|
|
54
|
-
|
|
55
|
-
### `useAppState(key, defaultValue?, removeOnUnmount?)`
|
|
56
|
-
|
|
57
|
-
React hook for managing global state - works exactly like `useState` but globally accessible.
|
|
58
|
-
|
|
59
|
-
```typescript
|
|
60
|
-
// Basic usage
|
|
61
|
-
const count = useAppState('count', 0)
|
|
62
|
-
|
|
63
|
-
// Auto-cleanup when component unmounts
|
|
64
|
-
const tempData = useAppState('tempData', null, true)
|
|
65
|
-
|
|
66
|
-
// TypeScript support
|
|
67
|
-
interface User {
|
|
68
|
-
name: string
|
|
69
|
-
email: string
|
|
70
|
-
}
|
|
71
|
-
const user = useAppState<User>('user', null)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### `setAppState(key, value)`
|
|
75
|
-
|
|
76
|
-
Update state from anywhere in your app - works with values or updater functions.
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
// Set value directly
|
|
80
|
-
setAppState('count', 42)
|
|
81
|
-
|
|
82
|
-
// Update with function (like useState)
|
|
83
|
-
setAppState('count', (prev) => prev + 1)
|
|
84
|
-
|
|
85
|
-
// Update object (shallow merge)
|
|
86
|
-
setAppState('user', { name: 'John' })
|
|
87
|
-
|
|
88
|
-
// Update array
|
|
89
|
-
setAppState('items', (prev) => [...prev, 'new item'])
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### `getAppState(key?)`
|
|
93
|
-
|
|
94
|
-
Get state value without subscribing to changes (no re-renders).
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
// Get specific key
|
|
98
|
-
const currentCount = getAppState('count')
|
|
99
|
-
|
|
100
|
-
// Get all state
|
|
101
|
-
const allState = getAppState()
|
|
102
|
-
|
|
103
|
-
// â ïļ Note: This doesn't trigger re-renders when state changes
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### `$getAppState` (Console Helper)
|
|
107
|
-
|
|
108
|
-
Debug helper available in browser console to inspect your app state.
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
// In browser console
|
|
112
|
-
$getAppState // Returns all state
|
|
113
|
-
$getAppState('key') // Returns specific key
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## ðŊ Advanced Usage
|
|
117
|
-
|
|
118
|
-
### Auto-cleanup with `removeOnUnmount`
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
function TemporaryComponent() {
|
|
122
|
-
// This state will be deleted when component unmounts
|
|
123
|
-
const [data, setData] = useAppState('tempData', [], true)
|
|
124
|
-
|
|
125
|
-
return <div>Temporary data: {data.length} items</div>
|
|
126
|
-
}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### TypeScript Support
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
interface CartItem {
|
|
133
|
-
id: string
|
|
134
|
-
name: string
|
|
135
|
-
price: number
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Type inference
|
|
139
|
-
const [cart, setCart] = useAppState<CartItem[]>('cart', [])
|
|
140
|
-
|
|
141
|
-
// Add item with type safety
|
|
142
|
-
const addItem = (item: CartItem) => {
|
|
143
|
-
setCart((prev) => [...prev, item])
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Performance: When to use `getAppState`
|
|
148
|
-
|
|
149
|
-
```typescript
|
|
150
|
-
// â
Good: Use useAppState for UI components
|
|
151
|
-
function Counter() {
|
|
152
|
-
const [count, setCount] = useAppState('count', 0)
|
|
153
|
-
return <div>Count: {count}</div> // Re-renders when count changes
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// â
Good: Use getAppState for utilities/callbacks
|
|
157
|
-
function logCurrentState() {
|
|
158
|
-
const count = getAppState('count')
|
|
159
|
-
console.log('Current count:', count) // No re-renders
|
|
160
|
-
}
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
## ð Comparison
|
|
164
|
-
|
|
165
|
-
| Feature | Hank-Zilla | Context API | Redux | Zustand |
|
|
166
|
-
| ----------------- | ----------- | ----------------- | ------------------ | ----------- |
|
|
167
|
-
| **Setup** | â None | â
Provider | â
Store + Actions | â
Store |
|
|
168
|
-
| **Bundle Size** | ðĒ ~1.3KB | ðĒ Built-in | ðī ~45KB | ðĄ ~8KB |
|
|
169
|
-
| **Global Access** | â
Anywhere | â Provider scope | â
Anywhere | â
Anywhere |
|
|
170
|
-
| **TypeScript** | ðĒ Full | ðĄ Manual | ðĒ Full | ðĒ Full |
|
|
171
|
-
|
|
172
|
-
## ð§ Migration
|
|
173
|
-
|
|
174
|
-
### From `useState` to `useAppState`
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
// Before: Local state
|
|
178
|
-
const [count, setCount] = useState(0)
|
|
179
|
-
|
|
180
|
-
// After: Global state
|
|
181
|
-
const count = useAppState('count', 0)
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### From Context API
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
// Before: Complex setup
|
|
188
|
-
const UserContext = createContext()
|
|
189
|
-
const { user, setUser } = useContext(UserContext)
|
|
190
|
-
|
|
191
|
-
// After: Direct usage
|
|
192
|
-
const user = useAppState('user', null)
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## ðĪ Contributing
|
|
196
|
-
|
|
197
|
-
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
198
|
-
|
|
199
|
-
## ð License
|
|
200
|
-
|
|
201
|
-
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
202
|
-
|
|
203
|
-
## ð Acknowledgments
|
|
204
|
-
|
|
205
|
-
Built with âĪïļ by [phamdung2209](https://github.com/phamdung2209) âĒ Special thanks to the React team for `useSyncExternalStore`
|
|
206
|
-
|
|
207
|
-
---
|
|
208
|
-
|
|
209
|
-
Made with ðĶ by the Hank-Zilla team
|
|
1
|
+
# ðĶ Hank-Zilla
|
|
2
|
+
|
|
3
|
+
A lightweight React state management library that eliminates the need for Context, Redux, or Providers. Share state across components as easily as `useState` with global accessibility.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/hank-zilla)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
|
|
9
|
+
## âĻ Features
|
|
10
|
+
|
|
11
|
+
- ð **No Provider Required**: Share state globally without wrapping components in Providers
|
|
12
|
+
- ð **Drop-in Replacement**: Replace `useState` with `useAppState` for global state
|
|
13
|
+
- ðŊ **Access Anywhere**: Use the same state in any component, anywhere in your app
|
|
14
|
+
- ðŠķ **Lightweight**: Zero dependencies, minimal bundle size (~2.1KB)
|
|
15
|
+
- ⥠**Fast**: Built on React's `useSyncExternalStore` for optimal performance
|
|
16
|
+
- ðĶ **TypeScript**: Full TypeScript support with type inference
|
|
17
|
+
|
|
18
|
+
## ðĶ Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install hank-zilla
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## ð Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { useAppState, setAppState, getAppState } from 'hank-zilla'
|
|
28
|
+
|
|
29
|
+
// Parent Component - Set state
|
|
30
|
+
function ParentComponent() {
|
|
31
|
+
const user = useAppState('user', { name: '', email: '' })
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div>
|
|
35
|
+
<button
|
|
36
|
+
onClick={() => setAppState('user', { name: 'John Doe', email: 'john@example.com' })}
|
|
37
|
+
>
|
|
38
|
+
Set User
|
|
39
|
+
</button>
|
|
40
|
+
<ChildComponent />
|
|
41
|
+
</div>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Child Component - Access same state (no props needed!)
|
|
46
|
+
function ChildComponent() {
|
|
47
|
+
const user = useAppState('user')
|
|
48
|
+
|
|
49
|
+
return <div>{user ? `Welcome, ${user.name}!` : 'No user logged in'}</div>
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## ð API Reference
|
|
54
|
+
|
|
55
|
+
### `useAppState(key, defaultValue?, removeOnUnmount?)`
|
|
56
|
+
|
|
57
|
+
React hook for managing global state - works exactly like `useState` but globally accessible.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Basic usage
|
|
61
|
+
const count = useAppState('count', 0)
|
|
62
|
+
|
|
63
|
+
// Auto-cleanup when component unmounts
|
|
64
|
+
const tempData = useAppState('tempData', null, true)
|
|
65
|
+
|
|
66
|
+
// TypeScript support
|
|
67
|
+
interface User {
|
|
68
|
+
name: string
|
|
69
|
+
email: string
|
|
70
|
+
}
|
|
71
|
+
const user = useAppState<User>('user', null)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `setAppState(key, value)`
|
|
75
|
+
|
|
76
|
+
Update state from anywhere in your app - works with values or updater functions.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// Set value directly
|
|
80
|
+
setAppState('count', 42)
|
|
81
|
+
|
|
82
|
+
// Update with function (like useState)
|
|
83
|
+
setAppState('count', (prev) => prev + 1)
|
|
84
|
+
|
|
85
|
+
// Update object (shallow merge)
|
|
86
|
+
setAppState('user', { name: 'John' })
|
|
87
|
+
|
|
88
|
+
// Update array
|
|
89
|
+
setAppState('items', (prev) => [...prev, 'new item'])
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `getAppState(key?)`
|
|
93
|
+
|
|
94
|
+
Get state value without subscribing to changes (no re-renders).
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Get specific key
|
|
98
|
+
const currentCount = getAppState('count')
|
|
99
|
+
|
|
100
|
+
// Get all state
|
|
101
|
+
const allState = getAppState()
|
|
102
|
+
|
|
103
|
+
// â ïļ Note: This doesn't trigger re-renders when state changes
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### `$getAppState` (Console Helper)
|
|
107
|
+
|
|
108
|
+
Debug helper available in browser console to inspect your app state.
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
// In browser console
|
|
112
|
+
$getAppState // Returns all state
|
|
113
|
+
$getAppState('key') // Returns specific key
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## ðŊ Advanced Usage
|
|
117
|
+
|
|
118
|
+
### Auto-cleanup with `removeOnUnmount`
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
function TemporaryComponent() {
|
|
122
|
+
// This state will be deleted when component unmounts
|
|
123
|
+
const [data, setData] = useAppState('tempData', [], true)
|
|
124
|
+
|
|
125
|
+
return <div>Temporary data: {data.length} items</div>
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### TypeScript Support
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
interface CartItem {
|
|
133
|
+
id: string
|
|
134
|
+
name: string
|
|
135
|
+
price: number
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Type inference
|
|
139
|
+
const [cart, setCart] = useAppState<CartItem[]>('cart', [])
|
|
140
|
+
|
|
141
|
+
// Add item with type safety
|
|
142
|
+
const addItem = (item: CartItem) => {
|
|
143
|
+
setCart((prev) => [...prev, item])
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Performance: When to use `getAppState`
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// â
Good: Use useAppState for UI components
|
|
151
|
+
function Counter() {
|
|
152
|
+
const [count, setCount] = useAppState('count', 0)
|
|
153
|
+
return <div>Count: {count}</div> // Re-renders when count changes
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// â
Good: Use getAppState for utilities/callbacks
|
|
157
|
+
function logCurrentState() {
|
|
158
|
+
const count = getAppState('count')
|
|
159
|
+
console.log('Current count:', count) // No re-renders
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## ð Comparison
|
|
164
|
+
|
|
165
|
+
| Feature | Hank-Zilla | Context API | Redux | Zustand |
|
|
166
|
+
| ----------------- | ----------- | ----------------- | ------------------ | ----------- |
|
|
167
|
+
| **Setup** | â None | â
Provider | â
Store + Actions | â
Store |
|
|
168
|
+
| **Bundle Size** | ðĒ ~1.3KB | ðĒ Built-in | ðī ~45KB | ðĄ ~8KB |
|
|
169
|
+
| **Global Access** | â
Anywhere | â Provider scope | â
Anywhere | â
Anywhere |
|
|
170
|
+
| **TypeScript** | ðĒ Full | ðĄ Manual | ðĒ Full | ðĒ Full |
|
|
171
|
+
|
|
172
|
+
## ð§ Migration
|
|
173
|
+
|
|
174
|
+
### From `useState` to `useAppState`
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
// Before: Local state
|
|
178
|
+
const [count, setCount] = useState(0)
|
|
179
|
+
|
|
180
|
+
// After: Global state
|
|
181
|
+
const count = useAppState('count', 0)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### From Context API
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
// Before: Complex setup
|
|
188
|
+
const UserContext = createContext()
|
|
189
|
+
const { user, setUser } = useContext(UserContext)
|
|
190
|
+
|
|
191
|
+
// After: Direct usage
|
|
192
|
+
const user = useAppState('user', null)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## ðĪ Contributing
|
|
196
|
+
|
|
197
|
+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
|
|
198
|
+
|
|
199
|
+
## ð License
|
|
200
|
+
|
|
201
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
202
|
+
|
|
203
|
+
## ð Acknowledgments
|
|
204
|
+
|
|
205
|
+
Built with âĪïļ by [phamdung2209](https://github.com/phamdung2209) âĒ Special thanks to the React team for `useSyncExternalStore`
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
Made with ðĶ by the Hank-Zilla team
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useEffect,useSyncExternalStore}from"react";import init$ from"./init$";const listeners=new Set,store={state:{},getState:()=>store.state,subscribe:t=>(listeners.add(t),()=>listeners.delete(t)),setState:(t,e)=>{store.state={...store.state,...Object.entries(t).reduce((t,[e,s])=>{const r=store.state[e];return t[e]="function"==typeof s?s(r):"object"==typeof s&&null!==s?Array.isArray(s)?Array.isArray(r)?[...r,...s]:[...s]:0===Object.keys(s).length?s:{...r||{},...s}:s,t},{})},e||listeners.forEach(t=>t())}};export const useAppState=(t,e,s=!1,r)=>(
|
|
1
|
+
import{useEffect,useSyncExternalStore}from"react";import init$ from"./init$";const listeners=new Set,store={state:{},getState:()=>store.state,subscribe:t=>(listeners.add(t),()=>listeners.delete(t)),setState:(t,e)=>{store.state={...store.state,...Object.entries(t).reduce((t,[e,s])=>{const r=store.state[e];return t[e]="function"==typeof s?s(r):"object"==typeof s&&null!==s?Array.isArray(s)?Array.isArray(r)?[...r,...s]:[...s]:0===Object.keys(s).length?s:{...r||{},...s}:s,t},{})},e||listeners.forEach(t=>t())}};export const useAppState=(t,e,s=!1,r)=>(t in store.state||void 0===e||store.setState({[t]:e},r),useEffect(()=>()=>{s&&deleteAppState(t)},[t,s]),useSyncExternalStore(store.subscribe,()=>store.state[t]??e,()=>e));export const setAppState=(t,e,s)=>{store.setState({[t]:e},s)};export const getAppState=t=>void 0===t?store.getState():store.getState()[t];export const deleteAppState=t=>{t in store.state&&delete store.state[t]};init$({getAppState:getAppState});export default store;
|