当前位置:网站首页>Mobx learning path - powerful "state management tool"
Mobx learning path - powerful "state management tool"
2022-06-25 17:22:00 【Bump】
Mobx Study
First episode :CSDN Bump , knowledge has no limit
Fine 33°
happy birthday to me.
2022/6/25
Mobx
A powerful state management tool
Mobx And redux The difference between
mobxWriting bias andoop- You can modify a piece of data directly , There is no need to always return a new data
- Not a single
store, Can be morereduxBy defaultjsNative objects store data , andmobxUse observable objects
Use
install
npm i [email protected]
Using strict mode
configure({
enforceActions:'always'
})
observable&&autorun
autorunUsed to listen for changes in valuesobservableUsed for definition
import React,{
Component} from 'react';
import {
observable,autorun} from 'mobx';
/** * Basic types */
// Listening for common types of data
let observableNumber=observable.box(66)
let observableDesc=observable.box(" knowledge has no limit ...")
// Execute once for the first time , Then the changes related to it will be executed again
autorun(()=>{
console.log(observableNumber.get())
})
setTimeout(() => {
observableNumber.set(11)
observableDesc.set(" Love learning .")
}, 2000);
/** * Object use map */
let myobj=observable.map({
name:" Bump ",
age:9999
})
autorun(()=>{
console.log(" object name Attributes changed "+myobj.get("name"))
})
class Zhuye extends Component{
render() {
return (
<div>
Mobx
</div>
);
};
};
export default Zhuye;
action
Specially modify observable value
import {
observable,autorun,action} from 'mobx'
const store=observable({
isTabbarShow:true,
list:[],
cityName:" Beijing ",
changeShow(){
this.isTabbarShow=true
},
changeHide(){
this.isTabbarShow=false
}
},{
changeHide:action,
changeShow:action// The two methods of marking are action, Specially modify observable value
})
export default store
Then you need to change isTabbarShow You can call the method in the place of …
Use decorator to write
import {
observable,autorun,action} from 'mobx'
class Store{
@observable isTabbarShow=true
@observable list=[]
@action changeShow(){
this.isTabbarShow=true
}
@action changeHide(){
this.isTabbarShow=false
}
}
const store=new Store()
export default store
Because the decorator writing method is not supported , It needs to support
- 1. Installation dependency :
npm i @babel/core @babel/plugin-proposal-decorators @babel/preset-env
- 2. establish .babelrc
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
]
]
}
- 3. establish
config-overrides.js
const path = require('path')
const {
override, addDecoratorsLegacy } = require('customize-cra')
function resolve(dir) {
return path.join(__dirname, dir)
}
const customize = () => (config, env) => {
config.resolve.alias['@'] = resolve('src')
if (env === 'production') {
config.externals = {
'react': 'React',
'react-dom': 'ReactDOM'
}
}
return config
};
module.exports = override(addDecoratorsLegacy(), customize())
- 4. Installation dependency
npm i customize-cra react-app-rewired
- 5. modify
package.json
----...
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
----....
vscode Need configuration
file — Preferences ---- Set up ----- Search for experimentalDecorators---- Tick this setting
Finally, re run it …
runInAction( asynchronous )
Asynchronous requests require , In strict mode
import axios from 'axios'
import {
observable,autorun,action,runInAction, configure} from 'mobx'
configure({
enforceActions:'always'
})
class Store{
@observable isTabbarShow=true
@observable list=[]
@action changeShow(){
this.isTabbarShow=true
}
@action changeHide(){
this.isTabbarShow=false
}
@action getList(){
axios({
method:'GET',
url:"https://m.maizuo.com/gateway?cityId=440300&ticketFlag=1&k=2005318",
headers:{
"X-Client-Info": '{"a":"3000","ch":"1002","v":"5.2.0","e":"1654433035923688551579649"}',
"X-Host": "mall.film-ticket.cinema.list"
}
}).then(res=>{
runInAction(()=>{
this.list=res.data.data.cinemas
})
})
}
}
const store=new Store()
export default store
So that it can be modified this.list Of course. ~
Mobx The basic use of is complete ~ Happy birthday, to me🤭
边栏推荐
- Pytorch official document learning record
- Redis Series - Overview day1 - 1
- vscode插件自用
- 2021年5月云南省网络空间安全比赛赛题复盘
- 软考中级和高级选哪个比较好?
- 心情
- Home office earned me C | community essay
- Mathematical modeling - nonlinear programming
- XXIX - orbslam2 real-time 3D reconstruction using realsensed435
- 巴比特 | 元宇宙每日荐读:三位手握“价值千万”藏品的玩家,揭秘数字藏品市场“三大套路”...
猜你喜欢

Kalman time series prediction

Mathematical modeling - nonlinear programming

Sword finger offer 50 First character that appears only once

A development of student management system based on PHP

Vscode plug-in self use

2022-06-17 advanced network engineering (x) is-is-general header, establishment of adjacency relationship, IIH message, DIS and pseudo node

Problems encountered in using MySQL

SDN系统方法 | 10. SDN的未来

旧手机变废为宝,充当服务器使用

Learning Tai Chi maker mqtt (II) basic principles of mqtt
随机推荐
Do you know all the configurations of pychrm?
How does social e-commerce operate and promote?
【黑苹果】联想拯救者Y70002019PG0
Sword finger offer II 014 A sliding window of anagrams in strings
Mathematical modeling - nonlinear programming
批量--07---断点重提
剑指 Offer II 035. 最小时间差
软考中级和高级选哪个比较好?
数学建模——整数规划
Win10开启热点共享后断网怎么解决?
大学生暑假换机热,ROG 明星产品幻 16 翻转版 / 幻 13 / 幻 X 预约
剑指 Offer II 014. 字符串中的变位词 滑动窗口
[micro service sentinel] overview of flow control rules | detailed explanation of flow control mode for source | < direct link >
MySQL 用 limit 为什么会影响性能?
学习太极创客 — MQTT(一)MQTT 是什么
How smart PLC constructs ALT instruction
效应与定律
ddia数据密集型应用系统设计 整理
Mobx学习之路----强大的“状态管理工具”
单例模式应用