You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -115,13 +117,20 @@ export function usePostList(filter: string) {
115
117
116
118
## Getting Started
117
119
120
+
First of all, let's create a database for daxus:
121
+
122
+
```ts
123
+
import { createDatabase } from'daxus';
124
+
exportconst db =createDatabase();
125
+
```
126
+
118
127
When using Daxus, you need to create models for different types of data. Taking our company as an example, the backend data includes posts, comments, forums, and more. You must create separate models for them when using Daxus.
119
128
120
129
Different models can use different data structures. For example, posts are suitable for storing data using a pagination data structure, while user settings may not be. You need to create different data structures for your models based on different requirements.
> The object returned by `createPaginationAdapter` provides not only the initial state but also various operation functions for handling pagination. This allows developers to manipulate pagination easily. Of course, you can design your own pagination if desired, as Daxus gives developers complete control over data.
After creating the model, you can start defining accessors. Accessors play a significant role in Daxus as they help fetch data from the server and synchronize it with your model once the data is obtained. Then, after your model is updated, it notifies the components that use the corresponding model to check if rerendering is necessary.
There are two type of the accessors. One is `normal`, the other one is `infinite`. You can use `model.defineNormalAccessor` to define a normal accessor, and use `model.defineInfiniteAccessor` to define an infinite accessor. Typically, you would only use `infinite` when implementing infinite loading. In most cases, `normal` is sufficient.
155
+
There are two type of the accessors. One is `normal`, the other one is `infinite`. You can use `model.defineAccessor` to define a normal accessor, and use `model.defineInfiniteAccessor` to define an infinite accessor. Typically, you would only use `infinite` when implementing infinite loading. In most cases, `normal` is sufficient.
146
156
147
-
The argument is the accessor's **action**. `fetchData` tells the accessor how to fetch data from the server, while `syncState` specifies how to synchronize the obtained data with the model's state.
157
+
The argument is the accessor's **action**. `name` is the name of the creator, `fetchData` tells the accessor how to fetch data from the server, while `syncState` specifies how to synchronize the obtained data with the model's state.
148
158
149
-
`defineNormalAccessor` and `defineInfiniteAccessor` returns an accessor creator function. Next, we will use the accessor created by `defineNormalAccessor` with the `useAccessor` hook.
159
+
`defineAccessor` and `defineInfiniteAccessor` returns an accessor creator function. Next, we will use the accessor created by `defineAccessor` with the `useAccessor` hook.
@@ -163,8 +172,7 @@ The second argument of `useAccessor` determines the shape of the `data`. You can
163
172
164
173
```typescript
165
174
function usePostTitle(id:number) {
166
-
const accessor =getPostById(id);
167
-
const { data } =useAccessor(accessor, state=>postAdapter.tryReadOne(state, id)?.title);
175
+
const { data } =useAccessor(getPostById(id), state=>postAdapter.tryReadOne(state, id)?.title);
168
176
169
177
returndata;
170
178
}
@@ -197,7 +205,8 @@ Daxus provides the `createPaginationAdapter` to help developers easily handle pa
197
205
Since pagination uses ID to reference all entities, when any entity updates, all paginations that include this entity will receive the latest data. Developers don't have to worry about inconsistent data across multiple lists.
Copy file name to clipboardExpand all lines: docs/accessor.md
+9-6Lines changed: 9 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,13 @@
1
1
# Model and Accessor
2
2
3
-
In Daxus, you need to create different models for different types of data. For example, you can create a model for post data and another model for comment data.
3
+
In Daxus, you need to create a database, then create different models for different types of data. For example, you can create a model for post data and another model for comment data.
4
4
5
5
The reason for creating different models is that different data may require different data structures. Pagination structure is suitable for post data, while it may not be as suitable for user data.
Daxus provides a pagination helper function called `createPaginationAdapter`. It helps you build and easily read/write pagination structures. For more information, you can refer to the [pagination page](./pagination.md).
@@ -18,7 +19,8 @@ Daxus provides a pagination helper function called `createPaginationAdapter`. It
18
19
Accessors play a crucial role in Daxus. Almost all functionality is built upon them, including deduplication, revalidation on focus, and subscribing to the state of the model.
To define an accessor, you need to provide two pieces of information to your model. The `fetchData` function specifies how to fetch the remote data, while the `syncState` function determines how to sync the fetched data to the state of the model.
39
+
To define an accessor, you need to provide three pieces of information to your model. The `name` property is the name of its creator. The `fetchData` function specifies how to fetch the remote data, while the `syncState` function determines how to sync the fetched data to the state of the model.
38
40
39
41
Moreover, you can specify `onSuccess` and `onError` to perform some side effect when fetching success or fail.
40
42
41
-
`defineNormalAccessor` returns an accessor creator that you can use to create an accessor. We encourage you to name the creator based on its purpose. In the example above, the accessor fetches post based on the given ID, so it is named `getPostById`.
43
+
`defineAccessor` returns an accessor creator that you can use to create an accessor. We encourage you to name the creator based on its purpose. In the example above, the accessor fetches post based on the given ID, so it is named `getPostById`.
42
44
43
-
> There is another method called `defineInfiniteAccessor` that is useful when implementing infinite scrolling. For more information, please refer to its [API documentation](./api/README.md#createmodel).
45
+
> There is another method called `defineInfiniteAccessor` that is useful when implementing infinite scrolling.
44
46
45
47
### `useAccessor`
46
48
@@ -63,6 +65,7 @@ When using the `useAccessor` hook, revalidation occurs in the following cases:
63
65
64
66
- The return value of `checkHasData` is `false`.
65
67
- The accessor is stale, and `revalidateIfStale` is `true`.
68
+
- The accessor is changed, and `revalidateOnMount` is `true`.
66
69
- The window regains focus, and `revalidateOnFocus` is `true`.
67
70
- The network is reconnected, and `revalidateOnReconnect` is `true`.
0 commit comments