@@ -14,14 +14,14 @@ slug: /react/hooks
14
14
15
15
## ` useAgile() `
16
16
17
- The ` useAgile() ` React Hook binds/subscribes States to Functional React Components.
17
+ The ` useAgile() ` React Hook binds/subscribes AgileTs States to Functional React Components for reactivity .
18
18
This binding ensures that the Component re-renders whenever a bound State changes.
19
19
We can flexibly bind any [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
20
20
(like States or Collections) to any React Component.
21
21
``` ts
22
- const myCoolState = useAgile (MY_COOL_STATE );
22
+ const myCoolState = useAgile (MY_COOL_STATE );
23
23
```
24
- The ` useAgile() ` Hook returns the current ` value ` of the provided State Instance
24
+ The ` useAgile() ` Hook returns the current ` value ` of the provided State
25
25
and ** not** the State Instance itself.
26
26
``` ts {5}
27
27
// -- core.js --------------------------------------------------
@@ -36,7 +36,7 @@ console.log(myState); // Returns 'jeff'
36
36
37
37
### 📚 Array
38
38
39
- We can also pass an array of State Instances into the ` useAgile() ` Hook.
39
+ We can also pass an array of States to the ` useAgile() ` Hook.
40
40
``` ts
41
41
useAgile ([MY_COOL_STATE1 , MY_COOL_STATE2 ]);
42
42
```
@@ -59,14 +59,14 @@ It can reduce the number of triggered re-render events by batching re-render job
59
59
Thereby, simultaneously triggered re-render events of different State Instances
60
60
are combined into one single (combined) re-render event
61
61
if these States share the same ` SubscriptionContainer ` .
62
- Each ` useAgile() ` Hook creates its own ` Subscription Container ` and registers it by AgileTs.
63
- ` Subscription Container ` serve as an interface to the React-Component for AgileTs.
62
+ Each ` useAgile() ` Hook creates its own ` Subscription Container ` and registers it with AgileTs.
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,
68
68
but also all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
69
- that contain an ` Observer ` .
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 ]);
72
72
```
@@ -94,9 +94,10 @@ Instances that contain an `Observer` are, for example:
94
94
console .log (myComputed ); // Returns 'hello there'
95
95
```
96
96
- ### [ ` Collection ` ] ( ../../core/api/collection/Introduction.md )
97
- ** Note:** The Collection has no own ` Observer ` .
97
+ ** Note:** A Collection doesn't contain directly an ` Observer ` .
98
98
But ` useAgile() ` is smart enough, to identify a Collection
99
- and binds the [ ` defualt ` Group] ( ../../core/api/collection/group/Introduction.md#-default-group ) to the Component instead.
99
+ and binds the [ ` defualt ` Group] ( ../../core/api/collection/group/Introduction.md#-default-group )
100
+ to the React Component instead.
100
101
The ` default ` Group represents the default pattern of the Collection.
101
102
``` ts {9}
102
103
// -- core.js --------------------------------------------------
@@ -160,7 +161,7 @@ Instances that contain an `Observer` are, for example:
160
161
const MY_STATE = createState (" Hello Stranger!" );
161
162
162
163
const RandomComponent = () => {
163
- const myFirstState = useAgile (MY_STATE ); // Returns "Hello Stranger!"
164
+ const myFirstState = useAgile (MY_STATE );
164
165
165
166
return (
166
167
<div >
@@ -199,7 +200,7 @@ console.log(typeof stringState); // Returns 'string'
199
200
200
201
| Prop | Type | Description | Required |
201
202
| ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
202
- | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useAgile Hook is located | Yes |
203
+ | ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Instances to be bound to the Functional Component. | Yes |
203
204
| ` config ` | [ AgileHookConfigInterface] ( ../../../Interfaces.md#agilehookconfiginterface ) | Configuration | No |
204
205
205
206
#### SubscribableAgileInstancesType
@@ -209,21 +210,23 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
209
210
210
211
### 📄 Return
211
212
212
- ` useAgile() ` returns the current ` output ` of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
213
+ The ` useAgile() ` Hook returns the current ` output `
214
+ of the specified [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
213
215
``` ts {5}
214
- const MY_STATE = App . createState (' jeff' );
216
+ const MY_STATE = createState (' jeff' );
215
217
216
218
// -- MyComponent.jsx ------------------------------------------
217
219
218
220
const myState = useAgile (MY_STATE );
219
221
console .log (myState ); // Returns 'jeff'
220
222
```
221
- When passing multiple Agile Sub Instances, an array of ` outputs ` matching the passed Instances is returned.
223
+ When passing multiple Agile Sub Instances,
224
+ an array of ` outputs ` matching the passed Instances is returned.
222
225
``` ts {6}
223
226
// -- core.js --------------------------------------------------
224
227
225
- const MY_STATE = App . createState (' jeff' );
226
- const MY_STATE_2 = App . createState (' frank' );
228
+ const MY_STATE = createState (' jeff' );
229
+ const MY_STATE_2 = createState (' frank' );
227
230
228
231
// -- MyComponent.jsx ------------------------------------------
229
232
@@ -244,29 +247,35 @@ console.log(myState2); // Returns 'frank'
244
247
245
248
## ` useProxy() `
246
249
250
+ ::: warning
251
+
252
+ Requires an additional package called ` @agile-ts/proxytree ` !
253
+
254
+ :::
255
+
247
256
The ` useProxy() ` is in its basic functionality equivalent to the [ ` useAgile() ` ] ( #useagile ) Hook.
248
257
It binds/subscribes AgileTs States to Functional React Components for reactivity.
249
- However, it has one advantage in terms of performance
250
- by only re-rendering the Component when an actual accessed property changes.
258
+ However, it has one advantage in terms of performance.
259
+ Because it only re-renders the React Component when an actual accessed property changes.
251
260
This is accomplished by warping a [ Proxy()] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy )
252
261
around the returned State value/s.
253
- Through these Proxy, AgileTs is able to exactly track accessed properties in the returned State value object/s
254
- and construct paths to these.
262
+ Through this Proxy, AgileTs is able to exactly track accessed properties in the React Component
263
+ and can construct paths to these. Based on these paths, it can select the particular accessed properties .
255
264
256
- Using the ` useAgile() ` , the Component would be always re-rendered on a subscribed State value change,
257
- regardless of whether the changed property value was accessed in the Component.
265
+ With the ` useAgile() ` the Component would always be re-rendered on a subscribed State value change,
266
+ regardless of whether the changed property value was accessed in the Component or not .
258
267
259
268
### 👀 Subscribable Instances
260
269
261
270
Not only AgileTs States can be bound to React Components,
262
271
but also all [ Agile Sub Instances] ( ../../../main/Introduction.md#agile-sub-instance )
263
- that contain an ` Observer ` .
272
+ that contain an [ ` Observer ` ] ( ../../../main/Introduction.md#observer ) .
264
273
``` ts
265
274
const [myCollection, myGroup, myState] = useProxy ([MY_COLLECTION , MY_GROUP , MY_STATE ]);
266
275
```
267
276
However, a [ Javascript Proxy] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy )
268
- can only be wrapped around values of the type object.
269
- Other provided instances are treated as in [ ` useAgile() ` ] ( #useagile ) .
277
+ can only be wrapped around values of the type ` object ` .
278
+ Instances that are not of the type object are treated as in [ ` useAgile() ` ] ( #useagile ) .
270
279
271
280
### 🔴 Example
272
281
@@ -285,7 +294,7 @@ const RandomComponent = () => {
285
294
<div >
286
295
<p >Name: { myState .name } </p >
287
296
<p >Rerender: { rerenderCount } </p >
288
- <p >State Value: { JSON .stringify (MY_STATE .value )} </p >
297
+ <p >State Value (not subscribed): < br /> { JSON .stringify (MY_STATE .value )} </p >
289
298
<button
290
299
onClick = { () => {
291
300
MY_STATE .patch ({name: generateId ()})
0 commit comments