Skip to content

Commit

Permalink
docs: remove unnecessary section in advanced.md [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 10, 2023
1 parent 083155c commit 3f7bc3d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
23 changes: 0 additions & 23 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,3 @@ userModel._fullname; // 报错了,找不到属性 _fullname
```

对外接口变得十分清爽,减少出错概率的同时,也提升了数据的安全性。

# 同步函数

没有人规定 methods 里的方法就必须是异步的,你可以随意写,只要是函数就行了。比如有时候一个模型里重复代码太多,提取通用的部分代码到 methods 里就很合适。或者组件里经常需要大量操作才能获得 state 里的一个数据,那么也建议放到 methods 里节省工作量。

```typescript
export const userModel = defineModel('users', {
initialState,
methods: {
getUsersAmount() {
return this.state.length;
},
getOther() {
return {
amount: this.getUsersAmount(),
other: 'xyz',
};
},
},
});

// const count = userModel.getUsersAmount();
```
4 changes: 1 addition & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#

我们已经迫不及待地想要把模型数据展示在React视图层了。为了让React能成功地订阅数据的变化,不同场景就会有不同的对接方式。老规矩,先讲最常用的。
# <!-- {docsify-ignore} -->

# useModel

Expand Down
7 changes: 6 additions & 1 deletion docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,23 @@ const userModel = defineModel('users', {
async get() {
const users = await http.get<UserItem[]>('/users');
this.setState(users);
return users;
},
async retrieve(id: number) {
const user = await http.get<UserItem>(`/users/${id}`);
this.setState((state) => {
state.push(user);
});
},
// 也可以是非异步的普通函数
findUser(id: number) {
return this.state.users.find((user) => user.id === id);
},
},
});
```

瞧见没,你可以在 methods 里自由地使用 async/await 方案,然后通过`this.setState`快速更新 state。
瞧见没,你可以在 methods 里自由地使用 async/await 方案,然后通过上下文`this.setState`快速更新 state。

接下来我们说说`setState`,这其实完全就是 reducers 的快捷方式,你可以直接传入数据或者使用匿名函数来操作,十分方便。这不禁让我们想起了 React Component 里的 setState?咳咳~~读书人的事,那能叫抄吗?

Expand Down

0 comments on commit 3f7bc3d

Please sign in to comment.