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

Commit 1af15c5

Browse files
committed
added useProxy example
1 parent 16ad917 commit 1af15c5

File tree

6 files changed

+103
-18
lines changed

6 files changed

+103
-18
lines changed

docs/examples/Indroduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ That's why we've put together a few examples for you.
1010
If that's not enough, you can find even more examples in the [AgileTs repository](https://github.com/agile-ts/agile/tree/master/examples).
1111

1212
## 🚀 Quick Links
13-
- [React](./react/All.mdx)
14-
- [React-Native](./react-native/All.mdx)
13+
- [React](./react/Introduction.md)
14+
- [React-Native](./react-native/Introduction.md)

docs/examples/react-native/Introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ slug: /examples/react-native
66
---
77

88
## 🤠 Get Started
9-
- [First State](./first-state)
9+
- [First State](./react-native/first-state)

docs/examples/react/Introduction.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ slug: /examples/react
66
---
77

88
## 🤠 Get Started
9-
- [First Collection](./first-collection)
10-
- [First Computed](./first-computed)
11-
- [First State](./first-state)
12-
- [First State [Class Component]](./class/first-state)
13-
- [Multieditor](./multieditor)
9+
- [First Collection](./react/first-collection)
10+
- [First Computed](./react/first-computed)
11+
- [First State](./react/first-state)
12+
- [First State [Class Component]](./react/class/first-state)
13+
- [Multieditor](./react/multieditor)
14+
- [useProxy](./react/simple-useproxy)
1415

1516
## 👾 Performance
16-
- [Large State](./large-state)
17+
- [Large State](./react/large-state)
1718

1819
You can get an overview of all performance examples in [this](https://github.com/agile-ts/performance-compare) repo.
1920

2021

2122
## 😎 Medium
22-
- [Simple Todo List](./simple-todo-list)
23+
- [Simple Todo List](./react/simple-todo-list)
2324

2425
## 🤓 Enterprise
2526
- coming soon
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
id: simple-useproxy
3+
title: Simple useProxy
4+
sidebar_label: Simple useProxy
5+
slug: /examples/react/simple-useproxy
6+
---
7+
8+
import {CodeSandBoxEmbed} from "../../../../src/components/docs/CodeSandBoxEmbed";
9+
10+
<CodeSandBoxEmbed uri={'agilets-useproxy-jw5x0'} />
11+

docs/packages/react/features/Hooks.md

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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 React Components**!
10+
Keep in mind that [React Hooks](https://reactjs.org/docs/hooks-intro.html) are only supported in **Functional React Components**!
1111

1212
:::
1313

@@ -170,11 +170,10 @@ The `useAgile()` Hook is almost 100% typesafe.
170170

171171
### 📭 Props
172172

173-
| Prop | Type | Description | Required |
174-
| ----------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
175-
| `deps` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useAgile Hook is located | Yes |
176-
| `key` | string \| number | Key/Name of SubscriptionContainer that is created. Mainly thought for Debugging | No |
177-
| `agileInstance` | Agile | To which Agile Instance the State belongs. Automatically detected if only one Agile Instance exists. | No |
173+
| Prop | Type | Description | Required |
174+
| ----------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ------------|
175+
| `deps` | Array<SubscribableAgileInstancesType\> \| SubscribableAgileInstancesType | Agile Sub Instances that are bound to the Component in which the useAgile Hook is located | Yes |
176+
| `config` | [AgileHookConfigInterface](../../../Interfaces.md#agilehookconfiginterface) | Configuration | No |
178177

179178
#### SubscribableAgileInstancesType
180179
```ts
@@ -214,6 +213,71 @@ console.log(myState2); // Returns 'frank'
214213

215214

216215

216+
## `useProxy()`
217+
218+
Basically `useProxy()` does the same as [`useAgile()`](#useagile)
219+
but it differs in one key area.
220+
It wraps a proxy around its return value,
221+
which has no limitation for the end user, but it allows AgileTs to track the used properties.
222+
With this information it is possible to only rerender the Component,
223+
when a used property mutates. In `useAgile()` it rerenders the Component
224+
whenever anything in the State changes, no matter it is displayed or not.
225+
Be aware that this is only useful if you pass an array or object State
226+
because it wouldn't make sense to track any primitive value.
227+
228+
### 🔴 Example
229+
230+
```tsx live
231+
const App = new Agile();
232+
const MY_STATE = App.createState({name: 'jeff', location: 'Germany', age: 19}, {key: 'myState'});
233+
234+
let rerenderCount = 0;
235+
236+
const RandomComponent = () => {
237+
const myState = useProxy(MY_STATE);
238+
239+
rerenderCount++;
240+
241+
return (
242+
<div>
243+
<p>Name: {myState.name}</p>
244+
<p>Rerender: {rerenderCount}</p>
245+
<p>State Value: {JSON.stringify(MY_STATE.value)}</p>
246+
<button
247+
onClick={() => {
248+
MY_STATE.patch({name: generateId()})
249+
}}
250+
>
251+
Update Name
252+
</button>
253+
<button
254+
onClick={() => {
255+
MY_STATE.patch({location: generateId()})
256+
}}
257+
>
258+
Update Location
259+
</button>
260+
</div>
261+
);
262+
}
263+
264+
render(<RandomComponent/>);
265+
```
266+
267+
### 🟦 Typescript
268+
269+
The `useProxy()` Hook is almost 100% typesafe.
270+
271+
272+
273+
<br />
274+
275+
---
276+
277+
<br />
278+
279+
280+
217281
## `useWatcher()`
218282
Creates a `callback` that observes the State on changes.
219283
The provided `callback` function will be fired on every State `value` mutation.
@@ -270,4 +334,4 @@ The `useWatcher()` Hook is almost 100% typesafe.
270334

271335
```ts
272336
void
273-
```
337+
```

docusaurus.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { Agile, generateId } = require('@agile-ts/core');
2-
const { AgileHOC, useAgile, useWatcher } = require('@agile-ts/react');
2+
const { AgileHOC, useAgile, useWatcher, useProxy } = require('@agile-ts/react');
33
const { Event, useEvent } = require('@agile-ts/event');
44
const { toast } = require('react-toastify');
55

@@ -39,6 +39,7 @@ const customFields = {
3939
liveCodeScope: {
4040
Agile,
4141
useAgile,
42+
useProxy,
4243
useEvent,
4344
useWatcher,
4445
AgileHOC,
@@ -156,6 +157,14 @@ const config = {
156157
label: 'Get Started',
157158
to: 'docs/introduction',
158159
},
160+
{
161+
label: 'Examples',
162+
to: 'docs/examples',
163+
},
164+
{
165+
label: 'React',
166+
to: 'docs/quick-start/react',
167+
},
159168
],
160169
},
161170
{

0 commit comments

Comments
 (0)