我觉得我得到了 mobx 的大部分内容,但我想澄清一些事情。我有一个带有一些可观察对象的商店,其核心是一个对象数组(打字稿):
class ClientStore {
constructor() {
this.loadClients();
}
@observable private _clients: IClient[] = [];
@computed get clients() {
return this._clients;
}
@observable isFetching: boolean = false;
@observable sortField: 'title' | 'modified' = 'modified';
@observable sortDescending: boolean = true;
getClientByUrlName(urlName: string): IClient {
return this._clients.find(c => c.urlName === urlName);
}
etc...
我的问题是最后一个函数——getClientByUrlName。由于这是从 observable 中发现的,因此任何使用该函数的 @observer react 组件都会正确重新渲染。这是惯用的 mobx 吗?感觉应该是一个计算值。我应该在想要使用它的组件中创建一个计算值吗?
//import singletone ClientStore
class ClientDetailsView extends React.Component<{params:{urlName:string}}, void> {
@computed get client() {
return ClientSotre.clients.find(c => c.urlName === this.props.params.urlName);
}
...rest of react component
我在这里寻找最佳实践和陷阱。任何帮助表示赞赏。
*编辑固定代码示例错误