当前位置:网站首页>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)
边栏推荐
猜你喜欢
分布式日志存储架构设计方案
最全总结Redis数据类型使用场景
看完这篇博客之后,含着泪学会了TCP/IP
【win10+cuda7.5+cudnn6.0安装caffe④】安装pycaffe
npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)
2021研究生数学建模D题,BP神经网络和卷积神经网络解题代码(基于pytorch)
简单做份西红柿炒蛋778
【转载】CMake 语法 - 详解 CMakeLists.txt
【网站小白】Hibernate插入数据成功,不报错,但是数据库中没有值
吃瓜教程task01 第1章 绪论
随机推荐
设计三级联动
C语言的二维数组初始化的几种方式介绍(私藏大数组初始化方式)
C语言自定义数据类型——联合体
开炮,开炮
Install different versions of MinGW (g++/gcc) and the configuration of the corresponding clion editor under Win
扩展运算符和剩余参数rest
[Verilog] I2S Master Test Bench
Some writing skills commonly used in Markdown
06-JS定时器:间隔定时器、延时定时器
[C language from elementary to advanced] Part 1 Initial C language (1)
LeetCode1166. Designing File Systems
C language file operation - detailed explanation of data file type, file judgment, and file buffer
How to open archives in administrator mode
数组的用法
09-ES6语法:变量、箭头函数、类语法、静态属性及非静态属性
Minecraft
Object.keys
第4章 复合类型-2(指针)
Chapter 4 Composite Types-1
第13章类继承