Store
可以在任何 Flux
系架构中找到,可以与 MVC 模式中的控制器进行比较。 Store
的主要职责是将逻辑和状态从组件中移至一个独立的,可测试的单元,这个单元在 JavaScript
前端和后端中都可以使用。
RootStore
import { observable, action, configure } from 'mobx';
// configure({ enforceActions: 'always' });
class RootStore {
@observable
name = '123';
@action('name')
updateCount() {
this.name = '456';
}
}
export default new RootStore();
Stores 注入
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'mobx-react';
import RootStore from './mobx/rootStore';
ReactDOM.render(
<Provider rootStore={RootStore}>
...
</Provider>,
document.getElementById('root')
);
页面引入
import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
@inject('rootStore')
@observe
class Index extends Component {
constructor(props) {
super(props);
}
render() {
return (
<>
</>
);
}
}
export default Index;
优点:
缺点:
RootStore
import { observable, action } from 'mobx';
import { configure } from 'mobx';
configure({ enforceActions: 'always' });
class RootStore {
@observable
test = '123';
@action('test')
updateCount() {
this.test = '456';
}
}
export default new RootStore();
页面 Store
import { observable, action } from 'mobx';
class DashBoardStore extends BasicStore {
@observable
name = 'Faker';
@action
updateName() {
this.name = 'Jiang';
}
}
export default DashBoardStore;
注入 RootStore
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'mobx-react';
import RootStore from './mobx/rootStore';
ReactDOM.render(
<Provider rootStore={RootStore}>
...
</Provider>,
document.getElementById('root')
);
页面引入
import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import DashboardStore from '../../mobx/Dashboard/store';
@inject('rootStore')
@observe
class Index extends Component {
constructor(props) {
super(props);
this.dashboardStore = new DashboardStore();
}
componentDidMount() {
this.dashboardStore.updateName();
}
render() {
return (
<>
</>
);
}
}
export default Index;
优点:
Store
,Store
不会非常庞大Store
相对独立RootStore
Store
初始化缺点:
React
组件原本的结构,例如所有需要响应数据变动的组件都需要使用 observer
装饰,组件本地状态也需要 observable
装饰,以及数据操作方式等等,对 Mobx
耦合较深,日后切换框架或重构的成本很高configure({ enforceActions: 'always' });
杜绝在 Action
以外对 Store
的修改RootStore
import UserStore from './User/store';
import StatisticalCenterStore from './StatisticalCenter/store';
import AccountSettingStore from './AccountSetting/store';
import CallRecordStore from './CallRecord/store';
class RootStore {
constructor() {
this.userStore = new UserStore();
this.statisticalCenterStore = new StatisticalCenterStore();
this.accountSettingStore = new AccountSettingStore();
this.callRecordStore = new CallRecordStore();
}
}
export default new RootStore();
Stores 注入
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'mobx-react';
import RootStore from './mobx/rootStore';
ReactDOM.render(
<Provider {...RootStore}>
...
</Provider>,
document.getElementById('root')
);
页面引入
import React from 'react';
import { inject, observer } from 'mobx-react';
@inject('userStore')
@inject('statisticalCenterStore')
@observe
class Index extends React.PureComponent {
constructor(props) {
super(props);
this.state = { };
}
render() {
return (
<>
</>
);
}
}
export default Index;
优点:
Store
,Store
不会非常庞大Store
相对独立Store
,引入即可缺点:
React
组件原本的结构,例如所有需要响应数据变动的组件都需要使用 observer
装饰,组件本地状态也需要 observable
装饰,以及数据操作方式等等,对 Mobx
耦合较深, 日后切换框架或重构的成本很高Store
,那么得写reset action
,一个个变量还原,当然也可以通过 mobx-state-tree
实现中性:
状态可以被随意修改:
configure({ enforceActions: 'always' });
杜绝在 Action
以外对 Store
的修改欢迎关注我的博客
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有