目录结构:

Model/index.js

'use strict';
import { action, autorun, observable, computed } from "mobx";
export default class TodoList {
@observable todos = [{ title: "test", finished: true }];
@observable data = [];
   constructor(){
    autorun(()=>{console.log(this.unfinishedTodoCount)});
   }
@computed get unfinishedTodoCount() {
return this.todos.filter(todo => !todo.finished).length;
}
@action getData() {
fetch("http://localhost/Server/index.php").then(res => res.json()).then(data => this.data = data);
}
@action addList() {
this.todos.push({ title: "test1", finished: false });
} }
  • @: es6新增的装饰器语法,babel已支持需要安装 babel-plugin-transform-decorators-legacy
  • 类的静态属性:es7新增的语法,babel已支持需要安装 babel-preset-stage-2
  • @observer: 让 React 组件自动起来,它会自动更新,即便是在一个很大的程序里也会工作的很好
  • @observable:监听数据,当数据发生改变的时候自动刷新视图
  • @computed: 创建自动运算的表达式。(一般用于计算)
  • @action:改变了@observable创建的数据,需要装饰action方法!(需要配合'use strict'使用,有助于更好地构建代码)(可以不适用action,但是不建议这样做)
  • autorun: 当@observable创建的数据发生改变时自动执行

View/index.js

import React,{Component} from "react";
import ReactDOM from "react-dom";
import {observer} from "mobx-react"; import TodoList from "../Model/index"; @observer
class TodoListView extends Component {
componentDidMount(){
this.props.todoList.getData();
}
clickHandle(){
this.props.todoList.addList();
}
render() {
return <div>
<ul>
{this.props.todoList.todos.map(todo =>
<TodoView todo={todo} key={todo.id} />
)}
</ul>
Tasks left: {this.props.todoList.unfinishedTodoCount}<br />
姓名:{this.props.todoList.data.name}<br />
年龄:{this.props.todoList.data.age}<br />
密码:{this.props.todoList.data.pass}<br />
<input name='name' type='button' value="按钮" onClick={this.clickHandle.bind(this)} />
</div>
}
} const TodoView = observer(({todo}) =>
<li>
<input
type="checkbox"
checked={todo.finished}
onClick={() => {return todo.finished = !todo.finished}}
/>{todo.title}
</li>
) const store = new TodoList();
ReactDOM.render(<TodoListView todoList={store} />, document.getElementById('container'));

 webpack.config.js

var webpack = require('webpack');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js'); module.exports = {
//插件项
// plugins: [commonsPlugin],
//页面入口文件配置
entry: {
index: './View/index.js'
},
//入口文件输出配置
output: {
path: 'dist/page',
filename: '[name].js'
},
module: {
//加载器配置
loaders: [
{ test: /\.css$/, loader: 'style-loader!css-loader' },
{ test: /\.js$/, loader: 'babel-loader' },
{ test: /\.(png|jpg)$/, loader: 'url-loader' }
]
},
};

.babelrc

{
"presets": ["react", "es2015", "stage-2"],
"plugins": [
"transform-decorators-legacy"
]
}

Page/index.html

<html>

<head>
<meta charset="utf-8" />
</head> <body>
<div id="container"></div>
<script src="../dist/page/index.js"></script>
</body> </html>

mobx react的更多相关文章

  1. MobX+react使用小demo

    第一次接触mobx,网上找了很多例子,写此主要总结一下create-react-app + mobx入门 create-react-app myreact cd myreact npm install ...

  2. [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer

    Applications are driven by state. Many things, like the user interface, should always be consistent ...

  3. mobx在react的使用

    ​ 创建项目第六步 mobx 1.安装 yarn add mobx yarn add mobx-react 2.新建/src/store/store.js import {observable, co ...

  4. React MobX 开始

    MobX 用于状态管理,简单高效.本文将于 React 上介绍如何开始,包括了: 了解 MobX 概念 从零准备 React 应用 MobX React.FC 写法 MobX React.Compon ...

  5. react学习一篇就够了

    webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...

  6. [Mobx] Use MobX actions to change and guard state

    This lesson explains how actions can be used to control and modify the state of your application. Th ...

  7. Mobx-React : 当前适合React的状态管理工具

    MobX 简单.可扩展的状态管理        MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商所赞助的.    安装 安装: npm install mobx ...

  8. React的状态管理工具

    Mobx-React : 当前最适合React的状态管理工具   MobX 简单.可扩展的状态管理        MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商 ...

  9. 【译】Redux 还是 Mobx,让我来解决你的困惑!

    原文地址:Redux or MobX: An attempt to dissolve the Confusion 原文作者:rwieruch 我在去年大量的使用了 Redux,但我最近都在使用 Mob ...

随机推荐

  1. C语言学习笔记--枚举&结构体

    枚举 枚举是一种用户定义的数据类型,它用关键字enum以如下语法格式来声明: enum 枚举类型名字 {名字0,名字1,...,名字n}: 枚举类型名字通常并不真的使用,要用的是大括号里面的名字,因为 ...

  2. UART接口

    1.UART UART(Universal Asynchronous Receiver and Transmitter)通用异步收发器(异步串行通信口),是一种通用的数据通信协议,它包括了RS232. ...

  3. 如何在一台机器上安装两个MYSQL数据库

    1.正常安装第一个mysql(安装步骤省略) 2.在控制面板里停止第一个mysql服务 3.将C:\Program Files\MySQL目录下的所有目录和文件copy到另外一个路径,我这里是copy ...

  4. opencv 用户文档 错误更正 仿射变换

    今天在看opencv官方给出的仿射变换计算仿射变换矩阵的文档的时候,发现官方文档中有个很明显的错误,再次给大家提个醒. 官方文档连接: http://opencv.willowgarage.com/d ...

  5. 14.4.3 Adaptive Hash Index 自适应hash index

    14.4.3 Adaptive Hash Index 自适应hash index 自适应hash index(AHI) 让InnoDB 执行更像内存数据库在系统使用合适的负载组合和足够的内存用于Buf ...

  6. ext2,ext3,ext4文件系统管理软件包e2fsprogs

    e2fsprogs(也叫做e2fs programs)是一个Ext2(及Ext3/4)文件系统工具集(Ext2 Filesystems Utilities[2] ),它包含了诸如创建.修复.配置.调试 ...

  7. POJ1942 Paths on a Grid(组合)

    题目链接. 分析: #include <cstdio> #include <iostream> #include <map> #include <cstrin ...

  8. 常用sql语句及案例(oracle)

    目录 1)基本 2)数学函数 3)rownum 4)分页 5)时间处理 6)字符函数 7)to_number 8)聚合函数 9)学生选课 10)图书馆借阅 基本 --新建表: ) ) not null ...

  9. lr 自带的例子,如何进行关联,通过代码的函数进行实现

    本篇主要介绍如何来进行把参数进行关联,首先对web tours进行设定 如下图 点击“administration”j进入跳转页面,如下图所示 勾选第三项,下拉下方,点击“update”按钮, 关闭浏 ...

  10. 值得收藏的Javascript代码

    1  Javascript数组转换为CSV格式 首先考虑如下的应用场景,有一个Javscript的字符型(或者数值型)数组,现在需要转换为以逗号分割的CSV格式文件.则我们可以使用如下的小技巧,代码如 ...