蚌埠网站建设电话,自学网站开发流程,邯郸网站制作地方,临沧市建设局网站React 中使用 mobx
配置开发环境 安装mobx和中间件工具 mobx-react-lite 只能函数组件中使用 yarn add mobx mobx-react-lite初始化 mobx 定义数据状态 state在构造器中实现数据响应式处理 makeAutoObservble定义修改数据的函数 action实例化 store 并导出 import { compute…React 中使用 mobx
配置开发环境 安装mobx和中间件工具 mobx-react-lite 只能函数组件中使用 yarn add mobx mobx-react-lite初始化 mobx 定义数据状态 state在构造器中实现数据响应式处理 makeAutoObservble定义修改数据的函数 action实例化 store 并导出 import { computed, makeAutoObservable } from mobx;class CounterStore {list [1, 2, 3, 4, 5, 6];constructor() {makeAutoObservable(this, {filterList: computed,});}// 修改原数组changeList () {this.list.push(7, 8, 9);};// 定义计算属性get filterList() {return this.list.filter((item) item 4);}
}const counter new CounterStore();export default counter;使用 store 在组件中导入 counterStore 实例对象在组件中使用 storeStore 实例对象中的数据通过事件调用修改数据的方法修改 store 中的数据让组件响应数据变化 // 导入counterStore
import counterStore from ./store;
// 导入observer方法
import { observer } from mobx-react-lite;
function App() {return (div classNameApp{/* 原数组 */}{JSON.stringify(counterStore.list)}{/* 计算属性 */}{JSON.stringify(counterStore.filterList)}button onClick{() counterStore.changeList()}change list/button/div);
}
// 包裹组件让视图响应数据变化
export default observer(App);异步处理数据 在 mobx 中编写异步请求方法 获取数据 存入 state 中组件中通过 useEffect 空依赖 触发 action 函数的执行 // 异步的获取import { makeAutoObservable } from mobx;
import axios from axios;class ChannelStore {channelList [];constructor() {makeAutoObservable(this);}// 只要调用这个方法 就可以从后端拿到数据并且存入channelListsetChannelList async () {const res await axios.get(http://XXX);this.channelList res.data.channels;};
}
const channlStore new ChannelStore();
export default channlStore;import { useEffect } from react;
import { useStore } from ./store;
import { observer } from mobx-react-lite;
function App() {const { channlStore } useStore();// 1. 使用数据渲染组件// 2. 触发action函数发送异步请求useEffect(() {channlStore.setChannelList();}, []);return (ul{channlStore.channelList.map((item) (li key{item.id}{item.name}/li))}/ul);
}
// 让组件可以响应数据的变化[也就是数据一变组件重新渲染]
export default observer(App);模块化 拆分模块 js 文件每个模块中定义自己独立的 state/action在 store/index.js 中导入拆分之后的模块进行模块组合利用 React 的 context 的机制导出统一的 useStore 方法给业务组件使用 1. store/taskStore.js
import { makeAutoObservable } from mobx;class TaskStore {taskList [];constructor() {makeAutoObservable(this);}addTask() {this.taskList.push(vue, react);}
}const task new TaskStore();export default task;2. store/counterStore.js
import { makeAutoObservable } from mobx;class CounterStore {count 0;list [1, 2, 3, 4, 5, 6];constructor() {makeAutoObservable(this);}addCount () {this.count;};changeList () {this.list.push(7, 8, 9);};get filterList() {return this.list.filter((item) item 4);}
}const counter new CounterStore();export default counter;3. 整合导出统一方法 store/index.js
import React from react;import counter from ./counterStore;
import task from ./taskStore;class RootStore {constructor() {this.counterStore counter;this.taskStore task;}
}const rootStore new RootStore();// context机制的数据查找链 Provider如果找不到 就找createContext方法执行时传入的参数
const context React.createContext(rootStore);const useStore () React.useContext(context);export { useStore };4. 组件中使用
import { observer } from mobx-react-lite;
// 导入方法
import { useStore } from ./store;
function App() {// 得到storeconst store useStore();return (div classNameAppbutton onClick{() store.counterStore.addCount()}{store.counterStore.count}/button/div);
}
// 包裹组件让视图响应数据变化
export default observer(App);
文章转载自: http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.smj78.cn.gov.cn.smj78.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.gmwdl.cn.gov.cn.gmwdl.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.ptxwg.cn.gov.cn.ptxwg.cn http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.dmcqy.cn.gov.cn.dmcqy.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.fpxsd.cn.gov.cn.fpxsd.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.clybn.cn.gov.cn.clybn.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.xirfr.cn.gov.cn.xirfr.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.jjzxn.cn.gov.cn.jjzxn.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.rjtmg.cn.gov.cn.rjtmg.cn http://www.morning.gcqs.cn.gov.cn.gcqs.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn