Mbox-React Native

学习网址:https://www.jianshu.com/p/bbf9837443f3

MboX环境配置:

.npm i mobx mobx-react --save //引入mobx
.npm i babel-plugin-transform-decorators-legacy babel-preset-react-native-stage- --save-dev //能够使用@标签
.在.babelrc文件中修改为{
"presets": ["react-native"],
"plugins": ["transform-decorators-legacy"]
}

mobx常用的标签

@observable: 使用此标签监控要检测的数据;
@observer: 使用此标签监控当数据变化是要更新的Component(组件类)
@action:使用此标签监控数据改变的自定义方法(当在需要数据改变的时候执行此自定义方法,
那么View层也会跟着自动变化,默认此View层已经使用@observer标签监控)

注:简单的数据,可以直接进行读取和复制操作,View也会变化;

/* * nornj&react配合应用 
导入相关依赖
* */
import React, {Component} from 'react';
import {
View,
StyleSheet,
ScrollView,
Text,
} from 'react-native'; /*
* 引入这个两个头文件
* */
import {observable, action} from 'mobx';
import {observer} from 'mobx-react/native'; /*
* 假数据
* */
const datas = [
{name:'苹果',count:0},
{name:'梨',count:0},
{name:'香蕉',count:0},
{name:'草莓',count:0},
{name:'橘子',count:0},
]; /*
* 对整个列表添加观察,观察列表个数的变化
* */
@observer
export default class MobxTestSecond extends Component { /*
* 数据管理器
* */
dataManager = new DataSource(); componentWillMount() {
/*
* 赋值初始数据
* */
this.dataManager.replace(datas);
} /*
* 添加一个新的Item
* */
addItem = () => {
let item = {name:'西瓜',count:0};
this.dataManager.addItem(item)
}; /*
* 删除第一个Item
* */
deleteItem = () => {
this.dataManager.deleteItem(0);
}; render() {
return (
<View style={styles.container}>
<View style={styles.addItemView}>
<Text style={styles.addItem} onPress={this.addItem}>增加</Text>
<Text style={styles.addItem} onPress={this.deleteItem}>删除</Text>
</View>
<ScrollView>
{
this.dataManager.dataSource.slice(0).map((item,i)=> <ItemView key = {i} item = {item}/>)
}
</ScrollView>
</View>
);
}
} /*
* 对每一个Item添加观察,改变个数
* */
@observer
class ItemView extends Component { countAdd = () => {
this.props.item.add();
}; countDec = () => {
this.props.item.dec();
}; render() {
const {item} = this.props;
return (
<View style={styles.itemContainer}>
<Text>{item.name}</Text>
<Text>{item.count}</Text>
<Text style={styles.btn} onPress={this.countAdd}> + </Text>
<Text style={styles.btn} onPress={this.countDec}> - </Text>
</View>
);
}
} /*
* 整个列表页数据管理器
* */
class DataSource {
// 本地数据源
@observable
dataSource = []; // 添加初始数据
@action
replace = (items) => {
// 1. 清空原数据
this.dataSource.splice(0, this.dataSource.length); // 2. 加载新数据
items.map((item, i) => {
this.dataSource.push(new Item(item));
});
}; // 添加新数据
@action
addItem = (item) => {
this.dataSource.unshift(new Item(item));
}; // 删除一条数据
@action
deleteItem = (idx) => {
this.dataSource.splice(idx, 1);
};
}
/*
* 单条Item数据管理器
* */
class Item { /*
* 商品名称(此值是不变的所以不需要检测此值)
* */
name; /*
* 监控商品个数
* */
@observable
count; constructor(item) {
this.name = item.name;
this.count = item.count;
}; /*
* 商品个数+1
* */
@action
add = () => {
this.count += 1;
}; /*
* 商品个数-1
* */
@action
dec= () => {
this.count > 0 && (this.count -= 1);
};
}

最新文章

  1. Digital calculation
  2. [HTML5]HTML结构性元素(Structure)
  3. 通过eclipse配置Spring MVC项目
  4. log4j常用配置以及日志文件保存位置
  5. SharePoint 2013 搜索体系结构
  6. STL容器set()---&gt;自定义数据类型
  7. Hadoop流程---从tpch到hive
  8. Java 遍历文件下jpg图片并解析图片
  9. python获取实时股票信息
  10. Android 插件化方案(动态加载)总结
  11. Linux下文件的mtime/atime/ctime研究
  12. 【翻译】了解Ext JS 5的小部件
  13. Ubuntu16.04安装opencv-3.4.2
  14. ZOJ 3949 Edge to the Root( 树形dp)
  15. C++ 命名管道 与Winform跨进程通信
  16. id选择器为变量时
  17. Pyhton对象解释
  18. Day7 错误和异常
  19. 梯度下降法的三种形式-BGD、SGD、MBGD
  20. P2167 [SDOI2009]Bill的挑战

热门文章

  1. STM32F407 正点原子按键输入实验
  2. De Moivre–Laplace theorem
  3. oracle数据库架构
  4. Arduino-原理图标识
  5. linux运维、架构之路-Hadoop完全分布式集群搭建
  6. [luogu]P1600 天天爱跑步[LCA]
  7. Spring Boot 中使用 spring-boot-devtools (使用 Gradle 作为构建工具)
  8. Windows XP SP2上安装.net 4
  9. SqlServer2012 File Table文件表
  10. loadrunner常用函数整理