Mobx:是一个可以和React良好配合的集中状态管理工具。
Mobx环境配置:Mobx式一个独立响应式的库,可以独立于任何UI框架。人们通常和React来半丁使用。用Mobx来做响应式的建模,React作为UI试图框架渲染内容。
Mobx的使用:
1.yarn add mobx mobx-react-lite。
2.在src文件下建立一个store文件夹,在store文件夹中建立count.js文件。
3.在store文件中创建一个count.js文件。
4.连接React。
Mobx的computed:
1.声明一个存在的数据。
2.定义一个get计算属性。
3.在makeAutoObservable方法标记。
Mobx模块化:index.js包含counter.js和list.js
在index.js文件中:
1.导入 import {ListStore} from './list.js’
import {Counter} from './counter'
2. class RootStore{
constructor(){
this.counterStore = new CounterStore()
this.listStore = new ListStore()
}
}
import React from 'react'
const rootStore = new RootStore();
const context = React.creatreContext(rootStore);
const useStore = () =>React.useContext(context);
export {useStore}