@@ -14,27 +14,30 @@ Be aware that [React Hooks](https://reactjs.org/docs/hooks-intro.html) are only
14
14
15
15
## ` useAgile() `
16
16
17
- The ` useAgile() ` React Hook, helps us to bind States to Functional React Components.
18
- This binding ensures that the Component rerender, whenever a bound State mutates.
17
+ The ` useAgile() ` React Hook binds/subscribes States to Functional React Components.
18
+ This binding ensures that the Component rerenders whenever a bound State mutates.
19
19
We can flexibly bind any State to any React Component.
20
20
``` ts
21
21
const myCoolState = useAgile (MY_COOL_STATE );
22
22
```
23
- ` useAgile() ` returns the current ` value ` of the passed State.
24
- ``` ts
23
+ Be aware, that ` useAgile() ` returns the current ` value ` of the passed State
24
+ and not the State Instance itself.
25
+ ``` ts {5}
25
26
const MY_STATE = App .createState (' jeff' );
26
27
27
28
// myComponent.jsx
28
29
29
30
const myState = useAgile (MY_STATE );
30
31
console .log (myState ); // Returns 'jeff'
31
32
```
32
- It is also possible to bind more than one State to a React Component at once.
33
+
34
+ ### 🗂 Array
35
+ ` useAgile() ` also supports ** arrays** of State Instances.
33
36
``` ts
34
- const [myCoolState1, myCoolState2] = useAgile ([MY_COOL_STATE1 , MY_COOL_STATE2 ]);
37
+ const [myCoolState1, myCoolState2] = useAgile ([MY_COOL_STATE1 , MY_COOL_STATE2 ]);
35
38
```
36
- Now ` useAgile() ` returns an array of State ` values ` that can be destructured.
37
- ``` ts
39
+ Now it returns an array of State ` values ` that can be destructured.
40
+ ``` ts {6}
38
41
const MY_STATE = App .createState (' jeff' );
39
42
const MY_STATE_2 = App .createState (' frank' );
40
43
@@ -44,22 +47,23 @@ const [myState, myState2] = useAgile([MY_STATE, MY_STATE_2]);
44
47
console .log (myState ); // Returns 'jeff'
45
48
console .log (myState2 ); // Returns 'frank'
46
49
```
47
- The binding of multiple State Instances at once has one advantage.
48
- It can lower the rerender count of the React Component.
49
- AgileTs can combine simultaneously triggered rerender of different States,
50
- if they share the same ` SubscriptionContainer ` .
50
+ Binding multiple States to a Component in a single ` useAgile() ` Hook has one advantage.
51
+ In some cases, it can lower the rerender count of the React Component.
52
+ This is due to the fact that simultaneously triggered rerender of different States can be combined into one single rerender
53
+ if the States share the same ` SubscriptionContainer ` .
51
54
Each ` useAgile() ` Hook creates its own ` SubscriptionContainer ` ,
52
- which serves as an interface to trigger render on the Component .
55
+ which serves as an interface to the Component in order to trigger rerender on it .
53
56
54
- ### Subscribable Instances
55
- We are not limited to States, we can bind any [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) that owns
56
- an ` observer ` to a React Component.
57
+ ### 🏷 Subscribable Instances
58
+ We are not limited to States.
59
+ We can bind any [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) that owns
60
+ an ` Observer ` to React Components.
57
61
``` ts
58
- const [myCollection, myGroup] = useAgile ([MY_COLLECTION , MY_GROUP ]);
62
+ const [myCollection, myGroup, myState ] = useAgile ([MY_COLLECTION , MY_GROUP , MY_STATE ]);
59
63
```
60
64
Instances that can be bound to a React Component via the ` useAgile() ` Hook:
61
65
- ### [ ` State ` ] ( ../../core/features/state/Introduction.md )
62
- ``` ts
66
+ ``` ts {5}
63
67
const MY_STATE = App .createState (' jeff' );
64
68
65
69
// myComponent.jsx
@@ -68,7 +72,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
68
72
console .log (myState ); // Returns 'jeff'
69
73
```
70
74
- ### [ ` Computed ` ] ( ../../core/features/computed/Introduction.md )
71
- ``` ts
75
+ ``` ts {5}
72
76
const MY_COMPUTED = App .createComputed (() => ' hello there' );
73
77
74
78
// myComponent.jsx
@@ -77,11 +81,11 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
77
81
console .log (myComputed ); // Returns 'hello there'
78
82
```
79
83
- ### [ ` Collection ` ] ( ../../core/features/collection/Introduction.md )
80
- ** Note:** The Collection has no own ` observer ` .
81
- But ` useAgile() ` is smart enough, to identify the Collection under the hood
84
+ ** Note:** The Collection has no own ` observer ` .
85
+ But ` useAgile() ` is smart enough, to identify a Collection under the hood
82
86
and binds the [ ` defualt ` Group] ( ../../core/features/collection/group/Introduction.md#-default-group ) to the Component instead.
83
87
The ` default ` Group represents the default pattern of the Collection.
84
- ``` ts
88
+ ``` ts {7}
85
89
const MY_COLLECTION = App .createCollection ({
86
90
initialData: [{id: 1 , name: ' a' }, {id: 2 , name: ' b' }, {id: 3 , name: ' c' }]
87
91
});
@@ -93,7 +97,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
93
97
// '[{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]'
94
98
```
95
99
- ### [ ` Group ` ] ( ../../core/features/collection/group/Introduction.md )
96
- ``` ts
100
+ ``` ts {8}
97
101
const MY_COLLECTION = App .createCollection ({
98
102
initialData: [{id: 1 , name: ' a' }, {id: 2 , name: ' b' }, {id: 3 , name: ' c' }]
99
103
});
@@ -105,7 +109,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
105
109
console .log (myGroup ); // Returns '[{id: 3, name: 'c'}, {id: 1, name: 'a'}]'
106
110
```
107
111
- ### [ ` Selector ` ] ( ../../core/features/collection/selector/Introduction.md )
108
- ``` ts
112
+ ``` ts {8}
109
113
const MY_COLLECTION = App .createCollection ({
110
114
initialData: [{id: 1 , name: ' a' }, {id: 2 , name: ' b' }, {id: 3 , name: ' c' }]
111
115
});
@@ -117,7 +121,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
117
121
console .log (mySelector ); // Returns '{id: 2, name: 'b'}'
118
122
```
119
123
- ### [ ` Item ` ] ( ../../core/features/collection/Introduction.md#-item )
120
- ``` ts
124
+ ``` ts {8}
121
125
const MY_COLLECTION = App .createCollection ({
122
126
initialData: [{id: 1 , name: ' a' }, {id: 2 , name: ' b' }, {id: 3 , name: ' c' }]
123
127
});
@@ -129,7 +133,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
129
133
console .log (myItem ); // Returns '{id: 3, name: 'c'}'
130
134
```
131
135
- ### ` undefined `
132
- ``` ts
136
+ ``` ts {1}
133
137
const myUndefined = useAgile (undefined );
134
138
console .log (myUndefined ); // Returns 'undefined'
135
139
```
@@ -138,40 +142,39 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
138
142
139
143
``` tsx live
140
144
const App = new Agile ();
141
- const MY_STATE = App .createState (" Hello Stranger!" );
142
-
143
- const RandomComponent = () => {
144
- const myFirstState = useAgile (MY_STATE ); // Returns "Hello Stranger!"
145
-
146
- return (
147
- <div >
148
- <p >{ myFirstState } </p >
149
- <button
150
- onClick = { () => {
151
- MY_STATE .set (" Hello Friend!" );
152
- }}
153
- >
154
- Update State
155
- </button >
145
+ const MY_STATE = App .createState (" Hello Stranger!" );
146
+
147
+ const RandomComponent = () => {
148
+ const myFirstState = useAgile (MY_STATE ); // Returns "Hello Stranger!"
149
+
150
+ return (
151
+ <div >
152
+ <p >{ myFirstState } </p >
153
+ <button
154
+ onClick = { () => {
155
+ MY_STATE .set (" Hello Friend!" );
156
+ }}
157
+ >
158
+ Update State
159
+ </button >
156
160
</div >
157
- );
158
- }
159
-
160
- render (<RandomComponent />);
161
+ );
162
+ }
163
+
164
+ render (<RandomComponent />);
161
165
```
162
166
163
167
### 🟦 Typescript
164
168
165
169
The ` useAgile() ` Hook is almost 100% typesafe.
166
- However, there are a few side cases you probably won't run into.
167
170
168
171
### 📭 Props
169
172
170
173
| Prop | Type | Description | Required |
171
174
| ----------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
172
175
| ` deps ` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useAgile Hook is located | Yes |
173
176
| ` key ` | string \| number | Key/Name of SubscriptionContainer that is created. Mainly thought for Debugging | No |
174
- | ` agileInstance ` | Agile | To which Agile Instance the State belongs. Automatically recognised if only one Agile Instance exists. | No |
177
+ | ` agileInstance ` | Agile | To which Agile Instance the State belongs. Automatically detected if only one Agile Instance exists. | No |
175
178
176
179
#### SubscribableAgileInstancesType
177
180
``` ts
@@ -180,8 +183,7 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
180
183
181
184
### 📄 Return
182
185
183
- ` useAgile() ` returns the current ` output ` of the passed [ Agile Sub Instance/s] ( ../../../main/Introduction.md#agile-sub-instance ) .
184
-
186
+ ` useAgile() ` returns the current ` output ` of the passed [ Agile Sub Instance] ( ../../../main/Introduction.md#agile-sub-instance ) .
185
187
``` ts {6,9}
186
188
const MY_STATE = App .State (1 );
187
189
const MY_STATE_2 = App .State (2 );
@@ -205,14 +207,18 @@ useAgile([MY_STATE, MY_STATE_2, MY_STATE_3]); // Returns [1, 2, 3]
205
207
206
208
207
209
## ` useWatcher() `
208
-
209
- With the ` useWatcher ` React Hook we are able to create a callback function that gets called whenever
210
- the passed State mutates. It's a synonym to the ` watch ` function, but might be cleaner to read in a React Component .
210
+ A ` callback ` that observes the State on changes.
211
+ The provided ` callback ` function will be fired on every State ` value ` mutation.
212
+ For instance if we update the State value from 'jeff' to 'hans' .
211
213
``` ts
212
- useWatcher (MY_STATE , () => {
213
- // This is a 'callback function' which gets called whenever MY_STATE mutates
214
+ useWatcher (MY_STATE , (value , key ) => {
215
+ console .log (value ); // Returns current State Value
216
+ console .log (key ); // Key of Watcher ("Aj2pB")
214
217
});
215
218
```
219
+ It is a synonym to the [ ` watch() ` ] ( ../../core/features/state/Methods.md#watch ) method.
220
+ But it has some advantages. It automatically cleans up the created watcher callback when the React Component unmounts
221
+ and might be cleaner to read in 'UI-Component-Code'.
216
222
217
223
### 🔴 Example
218
224
@@ -221,40 +227,39 @@ const App = new Agile();
221
227
const MY_STATE = App .createState (" hello" );
222
228
223
229
const RandomComponent = () => {
224
- useWatcher (MY_STATE , (value ) => {
225
- toast (" New Value: " + value );
226
- });
230
+ useWatcher (MY_STATE , (value ) => {
231
+ toast (" New Value: " + value );
232
+ });
227
233
228
- return (
229
- <div >
234
+ return (
235
+ <div >
230
236
<button
231
- onClick = { () => {
232
- MY_STATE .set (" bye" );
233
- }}
237
+ onClick = { () => {
238
+ MY_STATE .set (" bye" );
239
+ }}
234
240
>
235
- Change State
241
+ Change State
236
242
</button >
237
- </div >
238
- );
243
+ </div >
244
+ );
239
245
}
240
246
241
247
render (<RandomComponent />);
242
248
```
243
249
244
250
### 🟦 Typescript
245
251
246
- ` useWatcher ` is almost 100% typesafe.
252
+ The ` useWatcher() ` Hook is almost 100% typesafe.
247
253
248
254
### 📭 Props
249
255
250
256
| Prop | Type | Description | Required |
251
257
| ----------------- | ----------------------------------------------- | ---------------------------------------------------------------------------- | ------------|
252
- | ` state ` | State<T\> | State to which the passed watcher callback gets applied. | Yes |
253
- | ` callback ` | StateWatcherCallback<T\> | Callback Function that gets applied to the passed State | Yes |
258
+ | ` state ` | State<T\> | State to which the passed watcher callback is applied | Yes |
259
+ | ` callback ` | StateWatcherCallback<T\> | Callback function that is called on each State value change | Yes |
254
260
255
261
### 📄 Return
256
262
257
263
``` ts
258
264
void
259
- ```
260
-
265
+ ```
0 commit comments