react-state-bucket 1.0.8 → 1.0.9
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 +2 -2
- package/readme.md +237 -0
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.0.
|
|
2
|
+
"version": "1.0.9",
|
|
3
3
|
"name": "react-state-bucket",
|
|
4
4
|
"author": "Naxrul Ahmed",
|
|
5
5
|
"license": "MIT",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/react": "^19.0.2",
|
|
19
19
|
"@types/react-dom": "^19.0.2",
|
|
20
|
-
"makepack": "
|
|
20
|
+
"makepack": "^1.2.3",
|
|
21
21
|
"react": "^19.0.0",
|
|
22
22
|
"react-dom": "^19.0.0",
|
|
23
23
|
"typescript": "^4.4.2"
|
package/readme.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://mui.com/core/" rel="noopener" target="_blank"><img width="120" src="https://api.mypiebd.com/uploads/2024/11/26/images/5c31a5fc731a89c82fd0e846dd69a99378243.png" alt="Material UI logo"></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">React State Bucket</h1>
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
`react-state-bucket` is a lightweight and powerful package designed to manage states globally in React applications. It provides CRUD operations for your state data with ease, enabling developers to handle complex state management scenarios without the need for heavy libraries.
|
|
9
|
+
|
|
10
|
+
This package supports multiple storage options, including:
|
|
11
|
+
|
|
12
|
+
- Memory (default)
|
|
13
|
+
- Session Storage
|
|
14
|
+
- Local Storage
|
|
15
|
+
- URL Query Parameters
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install react-state-bucket
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- **Global State Management**: Easily manage state across your entire React application.
|
|
26
|
+
- **CRUD Operations**: Create, Read, Update, and Delete state values effortlessly.
|
|
27
|
+
- **Multiple Storage Options**: Choose where to store your data ("memory", "session", "local", or "url").
|
|
28
|
+
- **Reactivity**: Automatically update components when the state changes.
|
|
29
|
+
- **Custom Hooks**: Seamlessly integrate with React’s functional components.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Basic Example
|
|
34
|
+
|
|
35
|
+
```jsx
|
|
36
|
+
"use client";
|
|
37
|
+
|
|
38
|
+
import React from "react";
|
|
39
|
+
import {createBucket} from "react-state-bucket";
|
|
40
|
+
|
|
41
|
+
// Create a bucket with initial state
|
|
42
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
43
|
+
|
|
44
|
+
function App() {
|
|
45
|
+
const globalState = useGlobalState();
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<div>
|
|
49
|
+
<h1>Global State Management</h1>
|
|
50
|
+
<p>Count: {globalState.get("count")}</p>
|
|
51
|
+
<button onClick={() => globalState.set("count", globalState.get("count") + 1)}>Increment</button>
|
|
52
|
+
<button onClick={() => globalState.delete("count")}>Reset Count</button>
|
|
53
|
+
<pre>{JSON.stringify(globalState.getState(), null, 2)}</pre>
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default App;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Using Multiple Buckets
|
|
62
|
+
|
|
63
|
+
```jsx
|
|
64
|
+
const useUserBucket = createBucket({ name: "", age: 0 });
|
|
65
|
+
const useSettingsBucket = createBucket({ theme: "light", notifications: true });
|
|
66
|
+
|
|
67
|
+
function Profile() {
|
|
68
|
+
const userBucket = useUserBucket();
|
|
69
|
+
const settingsBucket = useSettingsBucket();
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<h1>User Profile</h1>
|
|
74
|
+
<button onClick={() => userBucket.set("name", "John Doe")}>Set Name</button>
|
|
75
|
+
<button onClick={() => settingsBucket.set("theme", "dark")}>Change Theme</button>
|
|
76
|
+
<pre>User State: {JSON.stringify(userBucket.getState(), null, 2)}</pre>
|
|
77
|
+
<p>Current Theme: {settingsBucket.get("theme")}</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Storage Options Example
|
|
84
|
+
|
|
85
|
+
```jsx
|
|
86
|
+
const usePersistentBucket = createBucket(
|
|
87
|
+
{ token: "", language: "en" },
|
|
88
|
+
{ store: "local" }
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
function PersistentExample() {
|
|
92
|
+
const persistentBucket = usePersistentBucket();
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div>
|
|
96
|
+
<h1>Persistent Bucket</h1>
|
|
97
|
+
<button onClick={() => persistentBucket.set("token", "abc123")}>Set Token</button>
|
|
98
|
+
<p>Token: {persistentBucket.get("token")}</p>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Reusing State Across Components
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
"use client";
|
|
108
|
+
|
|
109
|
+
import React from "react";
|
|
110
|
+
import {createBucket} from "react-state-bucket";
|
|
111
|
+
|
|
112
|
+
// Create a global bucket
|
|
113
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
114
|
+
|
|
115
|
+
function Counter() {
|
|
116
|
+
const globalState = useGlobalState(); // Access bucket functions and trigger re-render on state updates
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<div>
|
|
120
|
+
<h2>Counter Component</h2>
|
|
121
|
+
<p>Count: {globalState.get("count")}</p>
|
|
122
|
+
<button onClick={() => globalState.set("count", globalState.get("count") + 1)}>
|
|
123
|
+
Increment Count
|
|
124
|
+
</button>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function UserDisplay() {
|
|
130
|
+
const globalState = useGlobalState(); // Reuse the same global bucket for reactivity
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div>
|
|
134
|
+
<h2>User Component</h2>
|
|
135
|
+
<p>Current User: {globalState.get("user")}</p>
|
|
136
|
+
<button onClick={() => globalState.set("user", "John Doe")}>
|
|
137
|
+
Set User to John Doe
|
|
138
|
+
</button>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function App() {
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div>
|
|
147
|
+
<h1>Global State Example</h1>
|
|
148
|
+
<Counter />
|
|
149
|
+
<UserDisplay />
|
|
150
|
+
<pre>Global State: {JSON.stringify(useGlobalState().getState(), null, 2)}</pre>
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export default App;
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Direct Usage of Functions
|
|
159
|
+
|
|
160
|
+
The `createBucket` function provides direct access to the state management methods like `get`, `set`, `delete`, and others. For components that require re-rendering when the state changes, call the custom hook returned by `createBucket` within the component.
|
|
161
|
+
|
|
162
|
+
### Example
|
|
163
|
+
|
|
164
|
+
```jsx
|
|
165
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
166
|
+
|
|
167
|
+
function Counter() {
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<div>
|
|
171
|
+
<p>Count: {useGlobalState.get("count")}</p>
|
|
172
|
+
<button onClick={() => useGlobalState.set("count", useGlobalState.get("count") + 1)}>Increment</button>
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function App() {
|
|
178
|
+
useGlobalState(); // Will automatically re-render when state updates
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<div>
|
|
182
|
+
<Counter />
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export default App;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
## API Reference
|
|
193
|
+
|
|
194
|
+
### `createBucket(initial: object, option?: BucketOptions)`
|
|
195
|
+
|
|
196
|
+
Creates a new bucket for managing the global state.
|
|
197
|
+
|
|
198
|
+
#### Parameters
|
|
199
|
+
|
|
200
|
+
| Name | Type | Description |
|
|
201
|
+
| --------- | --------- | ----------------------------------------- |
|
|
202
|
+
| `initial` | `object` | Initial state values. |
|
|
203
|
+
| `option` | `object?` | Optional configuration (default: memory). |
|
|
204
|
+
|
|
205
|
+
#### `BucketOptions`
|
|
206
|
+
|
|
207
|
+
`BucketOptions` allows you to configure how and where the state is stored. It includes:
|
|
208
|
+
|
|
209
|
+
| Property | Type | Description |
|
|
210
|
+
| -------- | ------------------------------------- | ---------------------------------------------- |
|
|
211
|
+
| `store` | `'memory', 'session', 'local', 'url'` | Specifies the storage mechanism for the state. |
|
|
212
|
+
|
|
213
|
+
### Returned Functions
|
|
214
|
+
|
|
215
|
+
#### State Management
|
|
216
|
+
|
|
217
|
+
| Function | Description |
|
|
218
|
+
| ----------------- | ----------------------------------------------------- |
|
|
219
|
+
| `set(key, value)` | Sets the value of a specific key in the state. |
|
|
220
|
+
| `get(key)` | Retrieves the value of a specific key from the state. |
|
|
221
|
+
| `delete(key)` | Removes a key-value pair from the state. |
|
|
222
|
+
| `clear()` | Clears all state values. |
|
|
223
|
+
| `getState()` | Returns the entire state as an object. |
|
|
224
|
+
| `setState(state)` | Updates the state with a partial object. |
|
|
225
|
+
|
|
226
|
+
#### Change Detection
|
|
227
|
+
|
|
228
|
+
| Function | Description |
|
|
229
|
+
| ---------------- | ------------------------------------------- |
|
|
230
|
+
| `isChange(key)` | Checks if a specific key has been modified. |
|
|
231
|
+
| `getChanges()` | Returns an array of keys that have changed. |
|
|
232
|
+
| `clearChanges()` | Resets the change detection for all keys. |
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
|
237
|
+
|