Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit a1eb4bf

Browse files
committed
fixed typos
1 parent 976a266 commit a1eb4bf

File tree

1 file changed

+75
-70
lines changed

1 file changed

+75
-70
lines changed

docs/packages/react/features/Hooks.md

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,30 @@ Be aware that [React Hooks](https://reactjs.org/docs/hooks-intro.html) are only
1414

1515
## `useAgile()`
1616

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.
1919
We can flexibly bind any State to any React Component.
2020
```ts
2121
const myCoolState = useAgile(MY_COOL_STATE);
2222
```
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}
2526
const MY_STATE = App.createState('jeff');
2627

2728
// myComponent.jsx
2829

2930
const myState = useAgile(MY_STATE);
3031
console.log(myState); // Returns 'jeff'
3132
```
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.
3336
```ts
34-
const [myCoolState1, myCoolState2] = useAgile([MY_COOL_STATE1, MY_COOL_STATE2]);
37+
const [myCoolState1, myCoolState2] = useAgile([MY_COOL_STATE1, MY_COOL_STATE2]);
3538
```
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}
3841
const MY_STATE = App.createState('jeff');
3942
const MY_STATE_2 = App.createState('frank');
4043

@@ -44,22 +47,23 @@ const [myState, myState2] = useAgile([MY_STATE, MY_STATE_2]);
4447
console.log(myState); // Returns 'jeff'
4548
console.log(myState2); // Returns 'frank'
4649
```
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`.
5154
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.
5356

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.
5761
```ts
58-
const [myCollection, myGroup] = useAgile([MY_COLLECTION, MY_GROUP]);
62+
const [myCollection, myGroup, myState] = useAgile([MY_COLLECTION, MY_GROUP, MY_STATE]);
5963
```
6064
Instances that can be bound to a React Component via the `useAgile()` Hook:
6165
- ### [`State`](../../core/features/state/Introduction.md)
62-
```ts
66+
```ts {5}
6367
const MY_STATE = App.createState('jeff');
6468

6569
// myComponent.jsx
@@ -68,7 +72,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
6872
console.log(myState); // Returns 'jeff'
6973
```
7074
- ### [`Computed`](../../core/features/computed/Introduction.md)
71-
```ts
75+
```ts {5}
7276
const MY_COMPUTED = App.createComputed(() => 'hello there');
7377

7478
// myComponent.jsx
@@ -77,11 +81,11 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
7781
console.log(myComputed); // Returns 'hello there'
7882
```
7983
- ### [`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
8286
and binds the [`defualt` Group](../../core/features/collection/group/Introduction.md#-default-group) to the Component instead.
8387
The `default` Group represents the default pattern of the Collection.
84-
```ts
88+
```ts {7}
8589
const MY_COLLECTION = App.createCollection({
8690
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
8791
});
@@ -93,7 +97,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
9397
// '[{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]'
9498
```
9599
- ### [`Group`](../../core/features/collection/group/Introduction.md)
96-
```ts
100+
```ts {8}
97101
const MY_COLLECTION = App.createCollection({
98102
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
99103
});
@@ -105,7 +109,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
105109
console.log(myGroup); // Returns '[{id: 3, name: 'c'}, {id: 1, name: 'a'}]'
106110
```
107111
- ### [`Selector`](../../core/features/collection/selector/Introduction.md)
108-
```ts
112+
```ts {8}
109113
const MY_COLLECTION = App.createCollection({
110114
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
111115
});
@@ -117,7 +121,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
117121
console.log(mySelector); // Returns '{id: 2, name: 'b'}'
118122
```
119123
- ### [`Item`](../../core/features/collection/Introduction.md#-item)
120-
```ts
124+
```ts {8}
121125
const MY_COLLECTION = App.createCollection({
122126
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
123127
});
@@ -129,7 +133,7 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
129133
console.log(myItem); // Returns '{id: 3, name: 'c'}'
130134
```
131135
- ### `undefined`
132-
```ts
136+
```ts {1}
133137
const myUndefined = useAgile(undefined);
134138
console.log(myUndefined); // Returns 'undefined'
135139
```
@@ -138,40 +142,39 @@ Instances that can be bound to a React Component via the `useAgile()` Hook:
138142

139143
```tsx live
140144
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>
156160
</div>
157-
);
158-
}
159-
160-
render(<RandomComponent/>);
161+
);
162+
}
163+
164+
render(<RandomComponent/>);
161165
```
162166

163167
### 🟦 Typescript
164168

165169
The `useAgile()` Hook is almost 100% typesafe.
166-
However, there are a few side cases you probably won't run into.
167170

168171
### 📭 Props
169172

170173
| Prop | Type | Description | Required |
171174
| ----------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
172175
| `deps` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useAgile Hook is located | Yes |
173176
| `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 |
175178

176179
#### SubscribableAgileInstancesType
177180
```ts
@@ -180,8 +183,7 @@ type SubscribableAgileInstancesType = State | Collection | Observer | undefined;
180183

181184
### 📄 Return
182185

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).
185187
```ts {6,9}
186188
const MY_STATE = App.State(1);
187189
const MY_STATE_2 = App.State(2);
@@ -205,14 +207,18 @@ useAgile([MY_STATE, MY_STATE_2, MY_STATE_3]); // Returns [1, 2, 3]
205207

206208

207209
## `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'.
211213
```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")
214217
});
215218
```
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'.
216222

217223
### 🔴 Example
218224

@@ -221,40 +227,39 @@ const App = new Agile();
221227
const MY_STATE = App.createState("hello");
222228

223229
const RandomComponent = () => {
224-
useWatcher(MY_STATE, (value) => {
225-
toast("New Value: " + value);
226-
});
230+
useWatcher(MY_STATE, (value) => {
231+
toast("New Value: " + value);
232+
});
227233

228-
return (
229-
<div>
234+
return (
235+
<div>
230236
<button
231-
onClick={() => {
232-
MY_STATE.set("bye");
233-
}}
237+
onClick={() => {
238+
MY_STATE.set("bye");
239+
}}
234240
>
235-
Change State
241+
Change State
236242
</button>
237-
</div>
238-
);
243+
</div>
244+
);
239245
}
240246

241247
render(<RandomComponent/>);
242248
```
243249

244250
### 🟦 Typescript
245251

246-
`useWatcher` is almost 100% typesafe.
252+
The `useWatcher()` Hook is almost 100% typesafe.
247253

248254
### 📭 Props
249255

250256
| Prop | Type | Description | Required |
251257
| ----------------- | ----------------------------------------------- | ---------------------------------------------------------------------------- | ------------|
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 |
254260

255261
### 📄 Return
256262

257263
```ts
258264
void
259-
```
260-
265+
```

0 commit comments

Comments
 (0)