react-state-custom 1.0.11 → 1.0.13
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/API_DOCUMENTATION.md +883 -0
- package/README.md +180 -40
- package/dist/index.es.js +200 -979
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -22
- package/dist/index.umd.js.map +1 -1
- package/dist/state-utils/ctx.d.ts +4 -5
- package/dist/state-utils/utils.d.ts +4 -0
- package/package.json +5 -11
- package/src/state-utils/ctx.ts +27 -15
- package/src/state-utils/useQuickSubscribe.ts +1 -1
- package/src/state-utils/utils.ts +38 -0
- package/vite.config.ts +2 -2
package/README.md
CHANGED
|
@@ -1,57 +1,197 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React State Custom - Documentation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Welcome to the comprehensive documentation for the `react-state-custom` library!
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 📚 Documentation Files
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- **[API_DOCUMENTATION.md](./API_DOCUMENTATION.md)** - Complete API reference with examples for all exported functions, hooks, and classes
|
|
8
|
+
|
|
9
|
+
## 🚀 Quick Start
|
|
8
10
|
|
|
9
11
|
```bash
|
|
10
|
-
|
|
12
|
+
npm install react-state-custom
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
##
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
## 📖 What's Inside
|
|
16
|
+
|
|
17
|
+
The `react-state-custom` library provides a powerful set of tools for managing shared state in React applications:
|
|
18
|
+
|
|
19
|
+
### Core Features
|
|
20
|
+
|
|
21
|
+
- **Context System** - Type-safe context management with event-driven subscriptions
|
|
22
|
+
- **Root Context Factory** - Automated context lifecycle management
|
|
23
|
+
- **Auto Context System** - Self-managing context instances
|
|
24
|
+
- **Utility Hooks** - Performance optimization tools
|
|
25
|
+
|
|
26
|
+
### Key Benefits
|
|
27
|
+
|
|
28
|
+
- ✅ **Type Safety** - Full TypeScript support with strong typing
|
|
29
|
+
- ✅ **Performance** - Only re-renders when subscribed data changes
|
|
30
|
+
- ✅ **Flexibility** - Works with any data structure
|
|
31
|
+
- ✅ **Developer Experience** - Rich debugging and error checking
|
|
32
|
+
- ✅ **Minimal Boilerplate** - Automated context management
|
|
33
|
+
|
|
34
|
+
## 📝 Documentation Structure
|
|
35
|
+
|
|
36
|
+
The [API Documentation](./API_DOCUMENTATION.md) is organized into the following sections:
|
|
37
|
+
|
|
38
|
+
1. **Core Context System** - Basic context functionality
|
|
39
|
+
2. **Data Source Hooks** - Publishing data to contexts
|
|
40
|
+
3. **Data Subscription Hooks** - Subscribing to context changes
|
|
41
|
+
4. **Root Context Factory** - Advanced context patterns
|
|
42
|
+
5. **Auto Context System** - Automated context management
|
|
43
|
+
6. **Utility Hooks** - Performance and utility functions
|
|
44
|
+
7. **Usage Patterns** - Common implementation patterns
|
|
45
|
+
8. **Examples** - Complete application examples
|
|
46
|
+
|
|
47
|
+
## 🎯 Common Use Cases
|
|
48
|
+
|
|
49
|
+
- **Global State Management** - Application-wide state without Redux complexity
|
|
50
|
+
- **Component Communication** - Share data between distant components
|
|
51
|
+
- **Performance Optimization** - Minimize unnecessary re-renders
|
|
52
|
+
- **Context Composition** - Combine multiple contexts efficiently
|
|
53
|
+
|
|
54
|
+
## 🔧 Quick Example
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { useDataContext, useDataSource, useDataSubscribe } from 'react-state-custom';
|
|
58
|
+
|
|
59
|
+
interface AppState {
|
|
60
|
+
user: User | null;
|
|
61
|
+
theme: 'light' | 'dark';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Provider component
|
|
65
|
+
function AppProvider({ children }) {
|
|
66
|
+
const ctx = useDataContext<AppState>('app-state');
|
|
67
|
+
const user = useCurrentUser();
|
|
68
|
+
const theme = useTheme();
|
|
69
|
+
|
|
70
|
+
useDataSource(ctx, 'user', user);
|
|
71
|
+
useDataSource(ctx, 'theme', theme);
|
|
72
|
+
|
|
73
|
+
return <>{children}</>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Consumer component
|
|
77
|
+
function UserProfile() {
|
|
78
|
+
const ctx = useDataContext<AppState>('app-state');
|
|
79
|
+
const user = useDataSubscribe(ctx, 'user');
|
|
80
|
+
|
|
81
|
+
return <div>Hello, {user?.name}</div>;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
19
84
|
|
|
20
|
-
|
|
85
|
+
## 🔧 Additional Examples
|
|
86
|
+
|
|
87
|
+
### Using createRootCtx for Advanced State Management
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
import { createRootCtx, useDataSubscribeMultiple } from 'react-state-custom';
|
|
91
|
+
|
|
92
|
+
interface UserState {
|
|
93
|
+
user: User | null;
|
|
94
|
+
loading: boolean;
|
|
95
|
+
error: string | null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Create a state hook
|
|
99
|
+
function useUserState(props: { userId: string }) {
|
|
100
|
+
const [user, setUser] = useState<User | null>(null);
|
|
101
|
+
const [loading, setLoading] = useState(true);
|
|
102
|
+
const [error, setError] = useState<string | null>(null);
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
fetchUser(props.userId).then(setUser).catch(setError).finally(() => setLoading(false));
|
|
106
|
+
}, [props.userId]);
|
|
107
|
+
|
|
108
|
+
return { user, loading, error };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Create root context factory
|
|
112
|
+
const { Root: UserRoot, useCtxState: useUserCtxState } = createRootCtx(
|
|
113
|
+
'user-state',
|
|
114
|
+
useUserState
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Provider component
|
|
118
|
+
function UserProvider({ userId, children }: { userId: string; children: React.ReactNode }) {
|
|
21
119
|
return (
|
|
22
|
-
|
|
23
|
-
<
|
|
24
|
-
|
|
120
|
+
<>
|
|
121
|
+
<UserRoot userId={userId} />
|
|
122
|
+
{children}
|
|
123
|
+
</>
|
|
25
124
|
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Consumer component using useDataSubscribeMultiple
|
|
128
|
+
function UserProfile({ userId }: { userId: string }) {
|
|
129
|
+
const ctx = useUserCtxState({ userId });
|
|
130
|
+
const { user, loading, error } = useDataSubscribeMultiple(ctx, 'user', 'loading', 'error');
|
|
131
|
+
|
|
132
|
+
if (loading) return <div>Loading user...</div>;
|
|
133
|
+
if (error) return <div>Error: {error}</div>;
|
|
134
|
+
|
|
135
|
+
return <div>Welcome, {user?.name}!</div>;
|
|
136
|
+
}
|
|
29
137
|
```
|
|
30
138
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
139
|
+
### Using useQuickSubscribe for Simplified Access
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { useDataContext, useQuickSubscribe } from 'react-state-custom';
|
|
143
|
+
|
|
144
|
+
interface SettingsState {
|
|
145
|
+
theme: 'light' | 'dark';
|
|
146
|
+
language: string;
|
|
147
|
+
notifications: boolean;
|
|
148
|
+
updateSetting: (key: string, value: any) => void;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Component using useQuickSubscribe for easy property access
|
|
152
|
+
function SettingsPanel() {
|
|
153
|
+
const ctx = useDataContext<SettingsState>('settings');
|
|
154
|
+
|
|
155
|
+
// useQuickSubscribe automatically subscribes to accessed properties
|
|
156
|
+
const { theme, language, notifications, updateSetting } = useQuickSubscribe(ctx);
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div className={`settings-panel ${theme}`}>
|
|
160
|
+
<h2>Settings</h2>
|
|
161
|
+
|
|
162
|
+
<label>
|
|
163
|
+
Theme:
|
|
164
|
+
<select value={theme} onChange={(e) => updateSetting('theme', e.target.value)}>
|
|
165
|
+
<option value="light">Light</option>
|
|
166
|
+
<option value="dark">Dark</option>
|
|
167
|
+
</select>
|
|
168
|
+
</label>
|
|
169
|
+
|
|
170
|
+
<label>
|
|
171
|
+
Language:
|
|
172
|
+
<input
|
|
173
|
+
value={language}
|
|
174
|
+
onChange={(e) => updateSetting('language', e.target.value)}
|
|
175
|
+
/>
|
|
176
|
+
</label>
|
|
177
|
+
|
|
178
|
+
<label>
|
|
179
|
+
<input
|
|
180
|
+
type="checkbox"
|
|
181
|
+
checked={notifications}
|
|
182
|
+
onChange={(e) => updateSetting('notifications', e.target.checked)}
|
|
183
|
+
/>
|
|
184
|
+
Enable notifications
|
|
185
|
+
</label>
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
45
189
|
```
|
|
46
190
|
|
|
47
|
-
##
|
|
48
|
-
|
|
49
|
-
To build the library for production, run:
|
|
191
|
+
## 📄 License
|
|
50
192
|
|
|
51
|
-
|
|
52
|
-
yarn build
|
|
53
|
-
```
|
|
193
|
+
MIT License - see the main repository for details.
|
|
54
194
|
|
|
55
|
-
|
|
195
|
+
---
|
|
56
196
|
|
|
57
|
-
|
|
197
|
+
For the complete API reference with detailed examples, see [API_DOCUMENTATION.md](./API_DOCUMENTATION.md).
|