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

Commit 4c7ded6

Browse files
committed
optimize react introduction
1 parent 99ab7ae commit 4c7ded6

File tree

4 files changed

+129
-39
lines changed

4 files changed

+129
-39
lines changed

docs/packages/core/features/collection/group/Introduction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Most methods we use to modify, mutate and access the Group are chainable.
7373
MY_GROUP.undo().add(1).watch(() => {}).reset().persist().undo().remove(1).replace(2, 3);
7474
```
7575

76+
## 🍪 `default` Group
77+
todo
7678

7779
## 🔨 Use case
7880
For instance, we can use a Group to cluster a Post Collection into 'User Posts' of the different users.

docs/packages/react/Installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ npm install @agile-ts/react
2020
The `react` package is an extension of AgileTs and doesn't work without the [`core`](../core/Introduction.md) package,
2121
which functions as the brain of AgileTs and is indispensable.
2222
Unfortunately, we can't combine each `core` with `react` version.
23-
Therefore, we have created a table which shows which versions fit together without restrictions.
23+
Therefore, we have created a table that shows which versions fit together without restrictions.
2424

2525
| @agile-ts/react | @agile-ts/core | NPM Version | Supported React versions | Supports hook based components |
2626
| --------------- | ----------------------- | ------------------------ | -------------------------|---------------------------------- |
2727
| v0.0.7+ | v0.0.7+ | v6+ | 16.8+ | Yes |
2828
| v0.0.6 | v0.0.3 - v0.0.6 | v6+ | 16.8+ | Yes |
29-
_Other Versions aren't supported anymore_
29+
_Older Versions aren't supported anymore_

docs/packages/react/Introduction.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: Introduction
55
slug: /react
66
---
77

8-
> Integrate AgileTs into React or React-Native
8+
> Integration for React or React-Native
99
1010
<br />
1111

@@ -20,47 +20,48 @@ slug: /react
2020

2121
## `react`
2222

23-
The `react` package is an Integration of AgileTs into React.
23+
The `react` package integrates AgileTs into a [React](https://reactjs.org/) environment
24+
and serves as an Interface for AgileTs to React.
2425
Its main task is to bind States to React Components.
25-
This binding ensures that AgileTs rerender the Component, whenever a bound State mutates.
26-
It also offers some other useful functions that optimize the workflow with AgileTs in a React Environment.
26+
This binding ensures that AgileTs rerender the Component whenever a bound State mutates.
27+
It also offers some other valuable functionalities that optimize the workflow using AgileTs in a React environment.
2728

28-
A distinction is made between `Functional` and `Class` Components,
29-
as we prefer using `React Hooks` in Functional Components.
30-
But Hooks aren't supported in Class Components, so we came across other solutions,
31-
to offer the same features there too.
29+
A distinction is made between `Functional` and `Class` Components.
30+
As we prefer using [`React Hooks`](https://reactjs.org/docs/hooks-intro.html) in Functional Components,
31+
but Hooks aren't supported in Class Components.
32+
Therefore, we came across other solutions,
33+
to offer the same functionalities in Class Components too.
3234

3335
### 🐆 Functional Component
3436

35-
In Function Components we recommend using AgileTs Hooks like `useAgile`,
36-
which allows us to bind any State to our React Component
37+
In Functional Components we recommend using AgileTs Hooks like [`useAgile()`](./features/Hooks.md#useagile).
38+
The `useAgile()` Hook binds [Agile Sub Instances](../../main/Introduction.md#agile-sub-instance) (like States or Collections) to React Components.
3739
```ts
3840
// -- myComponent.jsx ------------------------------------------
3941

4042
// Binds MY_FIRST_STATE to myComponent
4143
const myFirstState = useAgile(MY_FIRST_STATE);
4244
```
43-
To find out more about `useAgile`, and other Hooks provided by AgileTs,
44-
checkout the AgileTs Hook [docs](./features/Hooks.md).
45+
To find out more about `useAgile()`, and other Hooks provided by AgileTs.
46+
Checkout the [AgileTs Hook documentation](./features/Hooks.md).
4547

4648
### 🦖 Class Component
4749

48-
In Class Component we currently only support the `AgileHOC`,
49-
which helps us binding States to our Component.
50-
It is a Higher order Component that gets wrapped around our React Component.
50+
In Class Components, we only provide the `AgileHOC`.
51+
The `AgileHOC` is a Higher Order Component that is wrapped around a React Component.
52+
It ensures the binding of [Agile Sub Instances](../../main/Introduction.md#agile-sub-instance) (like States or Collections) to React Components.
5153
```ts
5254
// -- myComponent.jsx ------------------------------------------
5355

5456
// Binds MY_FIRST_STATE to myComponent
5557
export default AgileHOC(myComponent, [MY_FIRST_STATE]);
5658
```
57-
To find out more AgileTs in Class Components,
58-
checkout the AgileHOC [docs](./features/AgileHOC.md).
59+
To find out more about the `AgileHOC` and AgileTs in Class Components,
60+
checkout the [AgileHOC documentation](./features/AgileHOC.md).
5961

6062
## 🚀 Quick Links
6163
- [Installation](./Installation.md)
6264
- [React Hook](./features/Hooks.md)
6365
- [useAgile](./features/Hooks.md#useagile)
6466
- [useWatcher](./features/Hooks.md#usewatcher)
65-
- [AgileHOC](./features/AgileHOC.md)
66-
67+
- [AgileHOC](./features/AgileHOC.md)

docs/packages/react/features/Hooks.md

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,129 @@ slug: /react/hooks
77

88
:::warning
99

10-
Be aware that [React Hooks](https://reactjs.org/docs/hooks-intro.html) are only supported in **Functional Components**!
10+
Be aware that [React Hooks](https://reactjs.org/docs/hooks-intro.html) are only supported in **Functional React Components**!
1111

1212
:::
1313

1414

1515
## `useAgile`
1616

17-
The `useAgile` React Hook, helps us to bind States to our React Component.
18-
These binding ensures that the Component rerender, whenever the bound State mutates.
19-
We can flexibly bind any State to our Component.
20-
It doesn't matter which State and how many States.
17+
The `useAgile()` React Hook, helps us to bind States to Functional React Components.
18+
This binding ensures that the Component rerender, whenever the bound State mutates.
19+
We can flexibly bind any State to any React Component.
2120
```ts
2221
const myCoolState = useAgile(MY_COOL_STATE);
2322
```
24-
`useAgile` returns the current _output_ of the passed State.
23+
`useAgile()` returns the current `value` of the passed State.
24+
```ts
25+
const MY_STATE = App.createState('jeff');
26+
27+
// myComponent.jsx
2528

26-
It is also possible to bind more than one State to our Component at once.
29+
const myState = useAgile(MY_STATE);
30+
console.log(myState); // Returns 'jeff'
31+
```
32+
It is also possible to bind more than one State at once to a React Component.
2733
```ts
28-
const [myCoolState1, myCoolStat2] = useAgile([MY_COOL_STATE1, MY_COOL_STATE2]);
34+
const [myCoolState1, myCoolState2] = useAgile([MY_COOL_STATE1, MY_COOL_STATE2]);
2935
```
30-
The binding of multiple State Instances, can lower the rerender count of our Component,
31-
because it allows AgileTs to combine two rerender triggered by different States at same point in time.
32-
Here `useAgile` returns the _output_ of the passed States, in the same order
33-
as they were passed.
36+
Now `useAgile()` returns an array of State `values` that can be destructured.
37+
```ts
38+
const MY_STATE = App.createState('jeff');
39+
const MY_STATE_2 = App.createState('frank');
40+
41+
// myComponent.jsx
42+
43+
const [myState, myState2] = useAgile([MY_STATE, MY_STATE_2]);
44+
console.log(myState); // Returns 'jeff'
45+
console.log(myState2); // Returns 'frank'
46+
```
47+
The binding of multiple State Instances at once has one advantage.
48+
It can lower the rerender count of the React Component,
49+
because AgileTs combines multiple at the same point in time triggered rerender of different States,
50+
if they have the same `SubscriptionContainer`. Each used `useAgile()` creates its own `SubscriptionContainer`.
3451

3552
We are not limited to States, we can bind any [Agile Sub Instance](../../../main/Introduction.md#agile-sub-instance) that owns
3653
an `observer` to a React Component.
3754
```ts
3855
const [myCollection, myGroup] = useAgile([MY_COLLECTION, MY_GROUP]);
3956
```
40-
[Agile Sub Instance](../../../main/Introduction.md#agile-sub-instance) with `observer`:
41-
- State
42-
- Group
43-
- Computed
44-
- Item
45-
- Collection (_exception_ since it has no `observer`)
57+
Instances that can be bound to a React Component via the `useAgile()` Hook:
58+
- ### [`State`](../../core/features/state/Introduction.md)
59+
```ts
60+
const MY_STATE = App.createState('jeff');
61+
62+
// myComponent.jsx
63+
64+
const myState = useAgile(MY_STATE);
65+
console.log(myState); // Returns 'jeff'
66+
```
67+
- ### [`Computed`](../../core/features/computed/Introduction.md)
68+
```ts
69+
const MY_COMPUTED = App.createComputed(() => 'hello there');
70+
71+
// myComponent.jsx
72+
73+
const myComputed = useAgile(MY_COMPUTED);
74+
console.log(myComputed); // Returns 'hello there'
75+
```
76+
- ### [`Collection`](../../core/features/collection/Introduction.md)
77+
**Note:** The Collection has no own `observer`.
78+
But `useAgile()` is smart enough, to identify the Collection under the hood
79+
and binds the [`defualt` Group](../../core/features/collection/group/Introduction.md#-default-group) to the Component instead.
80+
The `default` Group represents the default pattern of the Collection.
81+
```ts
82+
const MY_COLLECTION = App.createCollection({
83+
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
84+
});
85+
86+
// myComponent.jsx
87+
88+
const myCollection = useAgile(MY_COLLECTION);
89+
console.log(myCollection); // Returns (see below)
90+
// '[{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]'
91+
```
92+
- ### [`Group`](../../core/features/collection/group/Introduction.md)
93+
```ts
94+
const MY_COLLECTION = App.createCollection({
95+
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
96+
});
97+
const MY_GROUP = MY_COLLECTION.createGroup('myGroup', [3, 1]);
98+
99+
// myComponent.jsx
100+
101+
const myGroup = useAgile(MY_GROUP);
102+
console.log(myGroup); // Returns '[{id: 3, name: 'c'}, {id: 1, name: 'a'}]'
103+
```
104+
- ### [`Selector`](../../core/features/collection/selector/Introduction.md)
105+
```ts
106+
const MY_COLLECTION = App.createCollection({
107+
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
108+
});
109+
const MY_SELECTOR = MY_COLLECTION.select(2);
110+
111+
// myComponent.jsx
112+
113+
const mySelector = useAgile(MY_SELECTOR);
114+
console.log(mySelector); // Returns '{id: 2, name: 'b'}'
115+
```
116+
- ### [`Item`](../../core/features/collection/Introduction.md#-item)
117+
```ts
118+
const MY_COLLECTION = App.createCollection({
119+
initialData: [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}]
120+
});
121+
const MY_ITEM = MY_COLLECTION.getItem(3);
122+
123+
// myComponent.jsx
124+
125+
const myItem = useAgile(MY_ITEM);
126+
console.log(myItem); // Returns '{id: 3, name: 'c'}'
127+
```
128+
- ### `undefined`
129+
```ts
130+
const myUndefined = useAgile(undefined);
131+
console.log(myUndefined); // Returns 'undefined'
132+
```
46133

47134
### 🔴 Example
48135

0 commit comments

Comments
 (0)