@@ -23,7 +23,7 @@ const myCoolState = useAgile(MY_COOL_STATE);
23
23
```
24
24
The ` useAgile() ` Hook returns the current ` value ` of the provided State
25
25
and ** not** the State Instance itself.
26
- ``` ts {5 }
26
+ ``` ts {7 }
27
27
// -- core.js --------------------------------------------------
28
28
29
29
const MY_STATE = createState (' jeff' );
@@ -42,7 +42,7 @@ useAgile([MY_COOL_STATE1, MY_COOL_STATE2]);
42
42
```
43
43
Then ` useAgile() ` returns an array of State ` values ` matching the specified State Instances.
44
44
This array can be destructured in order to easily access the individual State values
45
- ``` ts {6 }
45
+ ``` ts {8 }
46
46
// -- core.js --------------------------------------------------
47
47
48
48
const MY_STATE = App .createState (' jeff' );
@@ -58,14 +58,14 @@ Binding multiple States to a React Component using a single `useAgile()` Hook ha
58
58
It can reduce the number of triggered re-render events by batching re-render jobs.
59
59
Thereby, simultaneously triggered re-render events of different State Instances
60
60
are combined into one single (combined) re-render event
61
- if these States share the same ` SubscriptionContainer ` .
61
+ if these States share the same ` Subscription Container ` .
62
62
Each ` useAgile() ` Hook creates its own ` Subscription Container ` and registers it with AgileTs.
63
63
Simply put ` Subscription Container ` serve as an interface to React-Components for AgileTs.
64
64
65
65
### 👀 Subscribable Instances
66
66
67
67
Not only AgileTs States can be bound to React Components via ` useAgile() ` ,
68
- but also all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
68
+ but also all other [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
69
69
that contain an [ ` Observer ` ] ( ../../../main/Introduction.md#observer ) .
70
70
``` ts
71
71
const [myCollection, myGroup, myState] = useAgile ([MY_COLLECTION , MY_GROUP , MY_STATE ]);
@@ -184,7 +184,7 @@ render(<RandomComponent/>);
184
184
### 🟦 Typescript
185
185
186
186
The ` useAgile() ` Hook is almost 100% typesafe.
187
- ``` ts
187
+ ``` ts {8}
188
188
// -- core.js --------------------------------------------------
189
189
190
190
const NUMBER_STATE = createState (0 );
@@ -201,7 +201,7 @@ console.log(typeof stringState); // Returns 'string'
201
201
202
202
| Prop | Type | Description | Required |
203
203
| ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
204
- | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Instances to be bound to the Functional Component. | Yes |
204
+ | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances to be bound to the Functional Component. | Yes |
205
205
| ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
206
206
207
207
#### SubscribableAgileInstancesType
@@ -212,9 +212,9 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
212
212
### 📄 Return
213
213
214
214
The ` useAgile() ` Hook returns the current ` output `
215
- or if the Instance has no ` output ` the ` value `
215
+ or if the Instance has no ` output ` the current ` value `
216
216
of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
217
- ``` ts {5 }
217
+ ``` ts {7 }
218
218
// -- core.js -------------------------------------------------
219
219
220
220
const MY_STATE = createState (' jeff' );
@@ -226,7 +226,7 @@ console.log(myState); // Returns 'jeff'
226
226
```
227
227
When passing multiple Agile Sub Instances,
228
228
an array of ` outputs ` /` values ` matching the passed Instances is returned.
229
- ``` ts {6 }
229
+ ``` ts {8 }
230
230
// -- core.js --------------------------------------------------
231
231
232
232
const MY_STATE = createState (' jeff' );
@@ -262,7 +262,7 @@ It binds/subscribes AgileTs States to a Functional React Component for reactivit
262
262
However, it has one advantage in terms of performance.
263
263
Because it only re-renders the React Component when an actual accessed property changes.
264
264
This is accomplished by warping a [ Proxy()] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy )
265
- around the returned State value/s.
265
+ around the returned State value object /s.
266
266
Through this Proxy, AgileTs is able to exactly track accessed properties in the React Component
267
267
and can construct paths to these. Based on these paths, it can select the particular accessed properties.
268
268
@@ -272,7 +272,7 @@ regardless of whether the changed property value was accessed in the Component o
272
272
### 👀 Subscribable Instances
273
273
274
274
Not only AgileTs States can be bound to React Components via ` useProxy() ` ,
275
- but also all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
275
+ but also all other [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
276
276
that contain an [ ` Observer ` ] ( ../../../main/Introduction.md#observer ) .
277
277
``` ts
278
278
const [myCollection, myGroup, myState] = useProxy ([MY_COLLECTION , MY_GROUP , MY_STATE ]);
@@ -328,7 +328,7 @@ The `useProxy()` Hook is almost 100% typesafe.
328
328
329
329
| Prop | Type | Description | Required |
330
330
| ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
331
- | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useProxy Hook is located | Yes |
331
+ | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances to be bound to the Functional Component. | Yes |
332
332
| ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
333
333
334
334
#### SubscribableAgileInstancesType
@@ -339,9 +339,9 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
339
339
### 📄 Return
340
340
341
341
The ` useProxy() ` Hook returns the current ` output `
342
- or if the Instance has no ` output ` the ` value `
342
+ or if the Instance has no ` output ` the current ` value `
343
343
of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
344
- ``` ts {5 }
344
+ ``` ts {7 }
345
345
// -- core.js -------------------------------------------------
346
346
347
347
const MY_STATE = createState (' jeff' );
@@ -353,16 +353,16 @@ console.log(myState); // Returns 'jeff'
353
353
```
354
354
When passing multiple Agile Sub Instances,
355
355
an array of ` outputs ` /` values ` matching the passed Instances is returned.
356
- ``` ts {6 }
356
+ ``` ts {8 }
357
357
// -- core.js --------------------------------------------------
358
358
359
- const MY_STATE = createState (' jeff' );
359
+ const MY_STATE = createState ({id: 1 : name : ' jeff' } );
360
360
const MY_STATE_2 = createState (' frank' );
361
361
362
362
// -- MyComponent.jsx ------------------------------------------
363
363
364
364
const [myState, myState2] = useProxy ([MY_STATE , MY_STATE_2 ]);
365
- console .log (myState ); // Returns 'jeff'
365
+ console .log (myState ); // Returns '{id: 1: name: ' jeff'} '
366
366
console .log (myState2 ); // Returns 'frank'
367
367
```
368
368
@@ -378,9 +378,9 @@ console.log(myState2); // Returns 'frank'
378
378
379
379
## ` useSelector() `
380
380
381
- The ` useSelector() ` React Hook binds/subscribes a part
381
+ The ` useSelector() ` React Hook binds/subscribes a ** part**
382
382
of an AgileTs State to a Functional React Component for reactivity.
383
- This binding ensures that the Component re-renders whenever the bound State part/property changes.
383
+ This binding ensures that the Component re-renders whenever the bound State ** part/property** changes.
384
384
The to bind part is selected via a selector function specified as second parameter.
385
385
``` ts
386
386
const myName = useAgile (MY_USER , (v ) => v .name );
@@ -389,7 +389,7 @@ const myName = useAgile(MY_USER, (v) => v.name);
389
389
### 👀 Subscribable Instances
390
390
391
391
Not only parts of AgileTs States can be bound to React Components via ` useSelector() ` ,
392
- but also parts of all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
392
+ but also parts of all other [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
393
393
that contain an [ ` Observer ` ] ( ../../../main/Introduction.md#observer ) .
394
394
``` ts
395
395
const myItem1 = useSelector (MY_COLLECTION , (v ) => v [' item1' ]);
@@ -436,18 +436,28 @@ render(<RandomComponent/>);
436
436
437
437
### 🟦 Typescript
438
438
439
- The ` useSelector() ` Hook isn't typesafe.
439
+ The ` useSelector() ` Hook isn't completely typesafe yet.
440
+ ``` ts
441
+ const selectedValue = useSelector (MY_STATE , (v ) => v .name );
442
+ ```
443
+ We are still figuring out how to automatically detect and return the selected property type.
444
+ As an override, you can also specify the individual types as generics.
440
445
``` ts
441
- const selectedValue = useSelector (MY_STATE , (v ) => v .name ) as string ;
446
+ useSelector < typeof MY_STATE . value , string > (MY_STATE , (v ) => v .name );
442
447
```
448
+ Or explicitly assign the desired type to the return value with the ` as ` keyword.
449
+ ``` ts
450
+ useSelector (MY_STATE , (v ) => v .name ) as string ;
451
+ ```
452
+
443
453
444
454
### 📭 Props
445
455
446
- | Prop | Type | Description | Required |
447
- | ----------------- | ---------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | ------------|
448
- | ` dep ` | SubscribableAgileInstancesType | Agile Sub Instance to be passed into the selector method | Yes |
449
- | ` selector ` | SelectorMethodType | Selector method to select the part/property to be bound to the Component in which the useSelector Hook is located | Yes |
450
- | ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
456
+ | Prop | Type | Description | Required |
457
+ | ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | ------------|
458
+ | ` dep ` | SubscribableAgileInstancesType | Agile Sub Instance to be passed into the specified selector method. | Yes |
459
+ | ` selector ` | SelectorMethodType | Selector method to select the part/property of the specified Agile Sub Instance value to be bound to the Functional Component. | Yes |
460
+ | ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
451
461
452
462
#### SubscribableAgileInstancesType
453
463
``` ts
@@ -461,9 +471,9 @@ type SelectorMethodType<T = any> = (value: T) => any;
461
471
462
472
### 📄 Return
463
473
464
- The ` useSelector() ` Hook returns the selected property
474
+ The ` useSelector() ` Hook returns the selected property (based on the selector method)
465
475
of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
466
- ``` ts {5 }
476
+ ``` ts {7 }
467
477
// -- core.js -------------------------------------------------
468
478
469
479
const MY_STATE = createState ({id: 10 , name: ' jeff' , age: 10 });
@@ -489,14 +499,14 @@ console.log(myName); // Returns 'jeff'
489
499
The ` useValue() ` is in its basic functionality equivalent to the [ ` useAgile() ` ] ( #useagile ) Hook.
490
500
It binds/subscribes AgileTs States to a Functional React Component for reactivity.
491
501
However, it differs in on key area,
492
- because it explicitly binds the ` value ` of a State or other [ Agile Sub Instance ] ( ../../../main/Introduction.md#agile-sub-instance )
493
- to the Component. With the ` useAgile() ` Hook,
502
+ because it explicitly binds the ` value ` of a State or other [ Agile Sub Instances ] ( ../../../main/Introduction.md#agile-sub-instance )
503
+ to the Component instead of preferring the ` ouptut ` . Normally (like in the ` useAgile() ` Hook) ,
494
504
the ` output ` of an Agile Sub Instance has a higher weight than the ` value ` .
495
505
496
506
For example if we bind a Collection with the ` useAgile() ` Hook to a React Component,
497
507
the ` output ` of the Collection is bound to the Component.
498
508
When we use the ` useValue() ` Hook instead, the ` value ` of the Collection is bound to the Component.
499
- ``` ts
509
+ ``` ts {9,12}
500
510
// -- core.js -------------------------------------------------
501
511
502
512
const MY_COLLECTION = createCollection ({
@@ -516,7 +526,7 @@ console.log(collectionOutput); // Returns (see below)
516
526
### 👀 Subscribable Instances
517
527
518
528
Not only AgileTs States can be bound to React Components via ` useValue() ` ,
519
- but also all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
529
+ but also all other [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
520
530
that contain an [ ` Observer ` ] ( ../../../main/Introduction.md#observer ) .
521
531
``` ts
522
532
const [myCollection, myGroup, myState] = useValue ([MY_COLLECTION , MY_GROUP , MY_STATE ]);
@@ -562,7 +572,7 @@ The `useValue()` Hook is almost 100% typesafe.
562
572
563
573
| Prop | Type | Description | Required |
564
574
| ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
565
- | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useProxy Hook is located | Yes |
575
+ | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances to be bound to the Functional Component. | Yes |
566
576
| ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
567
577
568
578
#### SubscribableAgileInstancesType
@@ -574,7 +584,7 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
574
584
575
585
The ` useValue() ` Hook returns the current ` value `
576
586
of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
577
- ``` ts {5 }
587
+ ``` ts {9 }
578
588
// -- core.js -------------------------------------------------
579
589
580
590
const MY_COLLECTION = createCollection ({initialData: [
@@ -588,7 +598,7 @@ console.log(myValue); // Returns '[1, 2]'
588
598
```
589
599
When passing multiple Agile Sub Instances,
590
600
an array of ` values ` matching the passed Instances is returned.
591
- ``` ts {6 }
601
+ ``` ts {10 }
592
602
// -- core.js --------------------------------------------------
593
603
594
604
const MY_COLLECTION = createCollection ({initialData: [
@@ -614,6 +624,7 @@ console.log(myState2); // Returns 'frank'
614
624
615
625
616
626
## ` useWatcher() `
627
+
617
628
Creates a ` callback ` that observes the State on changes.
618
629
The provided ` callback ` function will be fired on every State ` value ` mutation.
619
630
For instance if we update the State value from 'jeff' to 'hans'.
0 commit comments