react-state-bucket 1.1.0 β 1.1.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/package.json +1 -1
- package/readme.md +131 -135
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -4,192 +4,174 @@
|
|
|
4
4
|
|
|
5
5
|
<h1 align="center">React State Bucket</h1>
|
|
6
6
|
|
|
7
|
+
Effortlessly manage React application states with **react-state-bucket**, a lightweight yet powerful state management library.
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
---
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
## π Features
|
|
11
12
|
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
13
|
+
- **Global State Management**: Manage state across your entire React application with ease.
|
|
14
|
+
- **CRUD Operations**: Create, Read, Update, and Delete state values effortlessly.
|
|
15
|
+
- **Multiple Storage Options**: Store state in "memory," "session storage," "local storage," or "URL parameters."
|
|
16
|
+
- **Reactivity**: Automatically update components when the state changes.
|
|
17
|
+
- **Custom Hooks**: Seamlessly integrate with Reactβs functional components.
|
|
18
|
+
- **TypeScript Support**: Fully typed for a better development experience.
|
|
19
|
+
- **Lightweight**: Small bundle size with no unnecessary dependencies.
|
|
16
20
|
|
|
17
|
-
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## π¦ Installation
|
|
24
|
+
|
|
25
|
+
Install the package via npm or yarn:
|
|
18
26
|
|
|
19
27
|
```bash
|
|
28
|
+
# Using npm
|
|
20
29
|
npm install react-state-bucket
|
|
30
|
+
|
|
31
|
+
# Using yarn
|
|
32
|
+
yarn add react-state-bucket
|
|
21
33
|
```
|
|
22
34
|
|
|
23
|
-
|
|
35
|
+
---
|
|
24
36
|
|
|
25
|
-
|
|
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.
|
|
37
|
+
## π§ Setup and Usage
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
### Step 1: Create a State Bucket
|
|
32
40
|
|
|
33
|
-
|
|
41
|
+
Define your initial state and actions:
|
|
34
42
|
|
|
35
|
-
```
|
|
36
|
-
|
|
43
|
+
```javascript
|
|
44
|
+
import { createBucket } from 'react-state-bucket';
|
|
37
45
|
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
const initialState = {
|
|
47
|
+
count: 0,
|
|
48
|
+
user: 'Guest',
|
|
49
|
+
};
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
51
|
+
export const useGlobalState = createBucket(initialState);
|
|
52
|
+
```
|
|
43
53
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
### Step 2: Use the State Bucket in a Component
|
|
55
|
+
|
|
56
|
+
Access state and actions in your React components:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
import React from 'react';
|
|
60
|
+
import { useGlobalState } from './state';
|
|
61
|
+
|
|
62
|
+
const App = () => {
|
|
63
|
+
const globalState = useGlobalState();
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div>
|
|
67
|
+
<h1>Global State Management</h1>
|
|
68
|
+
<p>Count: {globalState.get('count')}</p>
|
|
69
|
+
<button onClick={() => globalState.set('count', globalState.get('count') + 1)}>Increment</button>
|
|
70
|
+
<button onClick={() => globalState.delete('count')}>Reset Count</button>
|
|
71
|
+
<pre>{JSON.stringify(globalState.getState(), null, 2)}</pre>
|
|
72
|
+
</div>
|
|
73
|
+
);
|
|
74
|
+
};
|
|
57
75
|
|
|
58
76
|
export default App;
|
|
59
77
|
```
|
|
60
78
|
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## π Advanced Features
|
|
82
|
+
|
|
61
83
|
### Using Multiple Buckets
|
|
62
84
|
|
|
63
|
-
```
|
|
64
|
-
const useUserBucket = createBucket({ name:
|
|
65
|
-
const useSettingsBucket = createBucket({ theme:
|
|
85
|
+
```javascript
|
|
86
|
+
const useUserBucket = createBucket({ name: '', age: 0 });
|
|
87
|
+
const useSettingsBucket = createBucket({ theme: 'light', notifications: true });
|
|
66
88
|
|
|
67
89
|
function Profile() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
90
|
+
const userBucket = useUserBucket();
|
|
91
|
+
const settingsBucket = useSettingsBucket();
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div>
|
|
95
|
+
<h1>User Profile</h1>
|
|
96
|
+
<button onClick={() => userBucket.set('name', 'John Doe')}>Set Name</button>
|
|
97
|
+
<button onClick={() => settingsBucket.set('theme', 'dark')}>Change Theme</button>
|
|
98
|
+
<pre>User State: {JSON.stringify(userBucket.getState(), null, 2)}</pre>
|
|
99
|
+
<p>Current Theme: {settingsBucket.get('theme')}</p>
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
80
102
|
}
|
|
81
103
|
```
|
|
82
104
|
|
|
83
|
-
### Storage Options
|
|
105
|
+
### Persistent Storage Options
|
|
84
106
|
|
|
85
|
-
```
|
|
107
|
+
```javascript
|
|
86
108
|
const usePersistentBucket = createBucket(
|
|
87
|
-
|
|
88
|
-
|
|
109
|
+
{ token: '', language: 'en' },
|
|
110
|
+
{ store: 'local' }
|
|
89
111
|
);
|
|
90
112
|
|
|
91
113
|
function PersistentExample() {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
114
|
+
const persistentBucket = usePersistentBucket();
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<div>
|
|
118
|
+
<h1>Persistent Bucket</h1>
|
|
119
|
+
<button onClick={() => persistentBucket.set('token', 'abc123')}>Set Token</button>
|
|
120
|
+
<p>Token: {persistentBucket.get('token')}</p>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
101
123
|
}
|
|
102
124
|
```
|
|
103
125
|
|
|
104
126
|
### Reusing State Across Components
|
|
105
127
|
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
import React from "react";
|
|
110
|
-
import {createBucket} from "react-state-bucket";
|
|
128
|
+
```javascript
|
|
129
|
+
import React from 'react';
|
|
130
|
+
import { createBucket } from 'react-state-bucket';
|
|
111
131
|
|
|
112
|
-
|
|
113
|
-
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
132
|
+
const useGlobalState = createBucket({ count: 0, user: 'Guest' });
|
|
114
133
|
|
|
115
134
|
function Counter() {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
</div>
|
|
126
|
-
);
|
|
135
|
+
const globalState = useGlobalState();
|
|
136
|
+
|
|
137
|
+
return (
|
|
138
|
+
<div>
|
|
139
|
+
<h2>Counter Component</h2>
|
|
140
|
+
<p>Count: {globalState.get('count')}</p>
|
|
141
|
+
<button onClick={() => globalState.set('count', globalState.get('count') + 1)}>Increment Count</button>
|
|
142
|
+
</div>
|
|
143
|
+
);
|
|
127
144
|
}
|
|
128
145
|
|
|
129
146
|
function UserDisplay() {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
</div>
|
|
140
|
-
);
|
|
147
|
+
const globalState = useGlobalState();
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<div>
|
|
151
|
+
<h2>User Component</h2>
|
|
152
|
+
<p>Current User: {globalState.get('user')}</p>
|
|
153
|
+
<button onClick={() => globalState.set('user', 'John Doe')}>Set User to John Doe</button>
|
|
154
|
+
</div>
|
|
155
|
+
);
|
|
141
156
|
}
|
|
142
157
|
|
|
143
158
|
function App() {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
);
|
|
159
|
+
return (
|
|
160
|
+
<div>
|
|
161
|
+
<h1>Global State Example</h1>
|
|
162
|
+
<Counter />
|
|
163
|
+
<UserDisplay />
|
|
164
|
+
<pre>Global State: {JSON.stringify(useGlobalState().getState(), null, 2)}</pre>
|
|
165
|
+
</div>
|
|
166
|
+
);
|
|
153
167
|
}
|
|
154
168
|
|
|
155
169
|
export default App;
|
|
156
170
|
```
|
|
157
171
|
|
|
158
|
-
|
|
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
|
|
172
|
+
---
|
|
163
173
|
|
|
164
|
-
|
|
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
|
|
174
|
+
## π API Reference
|
|
193
175
|
|
|
194
176
|
### `createBucket(initial: object, option?: BucketOptions)`
|
|
195
177
|
|
|
@@ -231,7 +213,21 @@ Creates a new bucket for managing the global state.
|
|
|
231
213
|
| `getChanges()` | Returns an array of keys that have changed. |
|
|
232
214
|
| `clearChanges()` | Resets the change detection for all keys. |
|
|
233
215
|
|
|
234
|
-
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## π€ Contributing
|
|
219
|
+
|
|
220
|
+
Contributions are welcome! Please check out the [contribution guidelines](https://github.com/devnax/react-state-bucket).
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## π License
|
|
235
225
|
|
|
236
226
|
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
|
237
227
|
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## π Support
|
|
231
|
+
|
|
232
|
+
For help or suggestions, feel free to open an issue on [GitHub](https://github.com/devnax/react-state-bucket/issues) or contact us via [devnaxrul@gmail.com](mailto:devnaxrul@gmail.com).
|
|
233
|
+
|