Skip to content

Commit af2f342

Browse files
Merge pull request #7 from HichemTab-tech/fix-docs
Update README to document scoped state management features
2 parents 96f124b + 699372d commit af2f342

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

README.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,31 +400,40 @@ Useful for SSR hydration, event listeners, debugging, imperative workflows.
400400
```ts
401401
import { sharedStatesApi, sharedFunctionsApi, sharedSubscriptionsApi } from 'react-shared-states';
402402

403-
// Preload state
403+
// Preload state (global scope by default)
404404
sharedStatesApi.set('bootstrap-data', { user: {...} });
405405

406+
// Preload state in a named scope
407+
sharedStatesApi.set('bootstrap-data', { user: {...} }, 'myScope');
408+
406409
// Read later
407-
const user = sharedStatesApi.get('bootstrap-data');
410+
const user = sharedStatesApi.get('bootstrap-data'); // global
411+
const userScoped = sharedStatesApi.get('bootstrap-data', 'myScope');
412+
413+
// Inspect all (returns nested object: { [scope]: { [key]: value } })
414+
console.log(sharedStatesApi.getAll());
408415

409-
// Inspect all
410-
console.log(sharedStatesApi.getAll()); // Map with prefixed keys
416+
// Clear all keys in a scope
417+
sharedStatesApi.clearScope('myScope');
411418

412419
// For shared functions
413420
const fnState = sharedFunctionsApi.get('profile-123');
421+
const fnStateScoped = sharedFunctionsApi.get('profile-123', 'myScope');
414422

415423
// For shared subscriptions
416424
const subState = sharedSubscriptionsApi.get('live-chat');
425+
const subStateScoped = sharedSubscriptionsApi.get('live-chat', 'myScope');
417426
```
418427

419428
## API summary:
420429

421-
| API | Methods |
422-
|--------------------------|---------------------------------------------------------------------------------------|
423-
| `sharedStatesApi` | `get(key, scope?)`, `set(key,val,scope?)`, `has`, `clear`, `clearAll`, `getAll()` |
424-
| `sharedFunctionsApi` | `get(key, scope?)` (returns fn state), `set`, `has`, `clear`, `clearAll`, `getAll()` |
425-
| `sharedSubscriptionsApi` | `get(key, scope?)` (returns sub state), `set`, `has`, `clear`, `clearAll`, `getAll()` |
430+
| API | Methods |
431+
|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
432+
| `sharedStatesApi` | `get(key, scopeName?)`, `set(key, val, scopeName?)`, `has(key, scopeName?)`, `clear(key, scopeName?)`, `clearAll()`, `clearScope(scopeName?)`, `getAll()` |
433+
| `sharedFunctionsApi` | `get(key, scopeName?)`, `set(key, val, scopeName?)`, `has(key, scopeName?)`, `clear(key, scopeName?)`, `clearAll()`, `clearScope(scopeName?)`, `getAll()` |
434+
| `sharedSubscriptionsApi` | `get(key, scopeName?)`, `set(key, val, scopeName?)`, `has(key, scopeName?)`, `clear(key, scopeName?)`, `clearAll()`, `clearScope(scopeName?)`, `getAll()` |
426435

427-
`scope` defaults to `"_global"`. Internally keys are stored as `${scope}_${key}`.
436+
`scopeName` defaults to `"_global"`. Internally, keys are stored as `${scope}//${key}`. The `.getAll()` method returns a nested object: `{ [scope]: { [key]: value } }`.
428437

429438

430439
## 🧩 Scoping Rules Deep Dive

0 commit comments

Comments
 (0)