当前位置:网站首页>Mobx--store状态管理工具
Mobx--store状态管理工具
2022-08-11 05:17:00 【-加油】
1、简单使用
安装
yarn add mobx-react-lite
counter.Store.js
import {
makeAutoObservable } from "mobx"
class CounterStore {
count = 0
constructor() {
//把数据弄成响应式
makeAutoObservable(this)
}
//定义action函数(修改数据)
addCount = () => {
this.count++
}
}
//导出给react使用
const counterStore = new CounterStore()
export {
counterStore }
react组件App.js中使用:
import {
counterStore } from "./store/counter.Store";
//导入中间件连接mobx 和 react 完成响应式变化
import {
observer} from "mobx-react-lite"
function App () {
return (
<div className="App">
{
counterStore.count}
<button onClick={
counterStore.addCount}>+</button>
</div>
)
}
//包裹App
export default observer(App)
2、含有计算属性
counter.Store.js
import {
computed, makeAutoObservable } from "mobx"
class CounterStore {
count = 0
list = [1, 2, 3, 4, 5, 6]
constructor() {
//把数据弄成响应式 标记filterList是计算属性
makeAutoObservable(this,{
filterList:computed
})
}
//计算属性
get filterList () {
return this.list.filter(item => item > 2)
}
//定义action函数(修改数据)
addList = () =>{
this.list.push(7,8,9)
}
//定义action函数(修改数据)
addCount = () => {
this.count++
}
}
//导出给react使用
const counterStore = new CounterStore()
export {
counterStore }
react组件App.js中使用:
使用时需要observer(App)
import {
counterStore } from "./store/counter.Store";
//导入中间件连接mobx 和 react 完成响应式变化
import {
observer} from "mobx-react-lite"
function App () {
return (
<div className="App">
{
/* 简单使用 */}
{
counterStore.count}
<button onClick={
counterStore.addCount}>+</button>
<br />
{
/* 使用计算属性 */}
{
counterStore.filterList.join('-')}
</div>
)
}
//包裹App
export default observer(App)
3、模块化
使用useContext机制导出。
模块counter.Store.js:
import {
makeAutoObservable } from "mobx"
class CounterStore {
count = 0
constructor() {
//把数据弄成响应式
makeAutoObservable(this)
}
//定义action函数(修改数据)
addCount = () => {
this.count++
}
}
//导出给react使用
export {
CounterStore }
模块list.Store.js
import {
makeAutoObservable } from "mobx"
class ListStore {
list = [1, 2, 3, 4, 5, 6]
constructor() {
makeAutoObservable(this)
}
//计算属性
get filterList () {
return this.list.filter(item => item > 2)
}
//定义action函数(修改数据)
addList = () => {
this.list.push(7, 8, 9)
}
}
export {
ListStore }
组合子模块index.js
使用useContext完成统一封装方法
import React from "react"
import {
ListStore } from "./list.Store.js"
import {
CounterStore } from "./counter.Store.js"
class RootStore {
constructor() {
// 对子模块进行实例化操作
//将来实例化new RootStore时,根实例有两个属性counterStore、listStore
this.counterStore = new CounterStore()
this.listStore = new ListStore()
}
}
//实例化操作
const rootStore = new RootStore()
//使用useContext完成统一封装方法 使用useContext查找机制:优先从Provider value查,
//如果查不到 就会找createContext(rootStore)传过来的默认参数rootStore
const context = React.createContext(rootStore)
//导出useStore方法 供组件通过useContext拿到根实例
// React.useContext 在 useStore 函数中调用。必须以单词“use”开头React钩子/钩子规则:useStore 不能userStore
const useStore = () => React.useContext(context)
export {
useStore }
react组件App.js中使用:
使用时需要observer(App)
import {
useStore } from "./store/index"
//导入中间件连接mobx 和 react 完成响应式变化
import {
observer} from "mobx-react-lite"
function App () {
//拿到根实例
const rootStore = useStore()
//使用解构赋值 也可以直接用rootStore.counterStore
const {
counterStore} = useStore()
// const {listStore} = useStore() rootStore.listStore
return (
<div className="App">
{
/* 简单使用 */}
{
counterStore.count}
<button onClick={
counterStore.addCount}>+</button>
<br />
{
rootStore.listStore.filterList.join('-')}
<button onClick={
rootStore.listStore.addList}>修改数组</button>
</div>
)
}
//包裹App
export default observer(App)
边栏推荐
猜你喜欢
随机推荐
C language file operation - detailed explanation of data file type, file judgment, and file buffer
C语言——文件操作函数 fseek、ftell、rewind详解
利用rand函数随机生成uuid
03-npm安装包详解,解决npm下载慢的问题,引入nrm等
C语言——动态内存分配常见的错误案例
深入理解线程、进程、多线程、线程池
Object.defineProperty新增/修改属性数据代理
全国青少年信息学奥林匹克联赛大纲
【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别
第13章类继承
uniapp中设置tabBar及其窗口标题
Some writing skills commonly used in Markdown
C语言——程序的编译与执行、宏定义详解
开炮,开炮
(2) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Jmeter)
Chapter 13 Class Inheritance
表单input控件数据双向绑定
在项目中使用flex布局的justify-content:space-around;遇到的问题,(数量为单数)
[Verilog] I2S Master Test Bench
Markdown 常用到的一些编写技巧