@leancodepl/utils 9.6.5 → 9.6.6
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/README.md +46 -0
- package/index.cjs.js +351 -282
- package/index.esm.js +351 -283
- package/package.json +2 -2
- package/src/index.d.ts +7 -11
- package/src/lib/hooks/index.d.ts +6 -0
- package/src/lib/valueContext.d.ts +52 -0
package/README.md
CHANGED
|
@@ -488,3 +488,49 @@ function MyComponent({ externalValue }: { externalValue: string }) {
|
|
|
488
488
|
return <div>{externalValue}</div>;
|
|
489
489
|
}
|
|
490
490
|
```
|
|
491
|
+
|
|
492
|
+
### `mkValueContext()`
|
|
493
|
+
|
|
494
|
+
Creates a React context hook for managing optional state values with Provider and setter utilities. Returns a hook with
|
|
495
|
+
attached Provider component and set function for declarative value management.
|
|
496
|
+
|
|
497
|
+
**Returns:** Hook function with attached `Provider` component and `set` function
|
|
498
|
+
|
|
499
|
+
**Usage:**
|
|
500
|
+
|
|
501
|
+
```typescript
|
|
502
|
+
import { mkValueContext } from "@leancodepl/utils";
|
|
503
|
+
|
|
504
|
+
const useTheme = mkValueContext<string>();
|
|
505
|
+
|
|
506
|
+
function App() {
|
|
507
|
+
return (
|
|
508
|
+
<useTheme.Provider initialValue="dark">
|
|
509
|
+
<ThemeConsumer />
|
|
510
|
+
</useTheme.Provider>
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function ThemeConsumer() {
|
|
515
|
+
const [theme] = useTheme();
|
|
516
|
+
return <div>Current theme: {theme}</div>;
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
**Using set to declaratively set context value:**
|
|
521
|
+
|
|
522
|
+
```typescript
|
|
523
|
+
import { mkValueContext } from "@leancodepl/utils";
|
|
524
|
+
|
|
525
|
+
const useActiveUser = mkValueContext<string>();
|
|
526
|
+
|
|
527
|
+
function UserProfile({ userId }: { userId: string }) {
|
|
528
|
+
useActiveUser.set(userId); // Sets value on mount, clears on unmount
|
|
529
|
+
return <div>Profile content</div>;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function UserBadge() {
|
|
533
|
+
const [activeUserId] = useActiveUser();
|
|
534
|
+
return <div>Active user: {activeUserId}</div>;
|
|
535
|
+
}
|
|
536
|
+
```
|