目录结构:

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. Linux shell (一)

    echo -e "Hello World! \a \n"     # -e 解析反斜杠 read -p "Please input your first name: &q ...

  2. PHP之路——验证码实现

    验证码生成并保存到session <?php //开始session session_start(); //画布大小 $image = imagecreate(100, 40); $color ...

  3. 值得IT运维人员警示的“一件事儿”

    昨天,一个用户打来了紧急求助电话,并且发了邮件,弄得我当时紧张了一下,以为他们那里又出了什么乱子.用户在电话里说:应用系统性能很差,运行很慢,几近“卡死”的感觉,而且重启了多次应用和数据库服务器,最终 ...

  4. github 提交报403 forbidden的错误解决

    github 提交报403 forbidden的错误解决 $ git push error: The requested URL returned error: 403 Forbidden while ...

  5. 01背包之求第K优解——Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639 题目大意是,往背包里赛骨头,求第K优解,在普通01背包的基础上,增加一维空间,那么F[i,v,k]可以理解 ...

  6. Android 各版本信息 (维基百科)

    The following tables show the release dates and key features of all Android operating system updates ...

  7. JS调试工具

    IE http://msdn.microsoft.com/zh-cn/library/ie/dn255003(v=vs.85).aspx FF http://www.wumii.com/item/1g ...

  8. Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)

    1判断select选项中 是否存在Value="paraValue"的Item 2向select选项中 加入一个Item 3从select选项中 删除一个Item 4删除selec ...

  9. GC与显式内存管理

    C++复兴的话题至今已被鼓吹两年有余,Herb Sutter和Bjarne Stroustrup等大牛们也为C++带来了大步伐的革新.然而,从这两年的效果而言,C++的复兴并没有发生.一方面随着世界经 ...

  10. LaTex希腊字母

    Name Symbol Command Alpha $\alpha$ $A$ \alpha A Beta $\beta$ $B$ \beta B Gamma $\gamma$ $\Gamma$ \ga ...