目录结构:

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. codevs 1107 等价表达式

    传送门 题解:第一眼这题好像非常难得样子,简直没有思路.但是这可以用栈带入特殊值来解决.这里用到两个栈,一个是存贮数字,另一个存贮运算符,按优先级进行运算.当读入的运算符比运算符栈的栈顶元素优先级低时 ...

  2. 转:你需要知道的NoSQL数据库10件事

    你需要知道的NoSQL数据库10件事 NoSQL的5个优势 1.弹性扩展 多年来,数据库管理员一直依赖于向上扩展(scale up)-随着数据库负载的增加购买更大的数据库服务器―而不是向外扩展-随着负 ...

  3. Java进阶代码

    本文重在温习……不过初学以及进阶高手不可错过 1.  public static void arraycopy(全小写)(object src,int srcPos,object dest,int d ...

  4. 开发者应该避免使用的6个Java功能(转)

    本文作者是一名拥有多年Java开发经验的程序员,他从经验中得出,并不是所有的Java SE功能/API都值得程序员去使用,比如本文列举的这6个,大家在使用前得慎重对待.以下是对原文的摘译. 多年的Ja ...

  5. Go语言开发环境安装

    Go是Google开发的一种编译型,並發型,并具有垃圾回收功能的编程语言. 去http://golang.org/doc/install#download 下载相应的版本. 1.安装go语言:2.将g ...

  6. POJ1080 Human Gene Functions(LCS)

    题目链接. 分析: 和 LCS 差不多. #include <iostream> #include <cstdio> #include <cstdlib> #inc ...

  7. 【转】(DT系列二)device tree的书写规范

    原文网址:http://www.cnblogs.com/biglucky/p/4057478.html devicetree的书写规范 下面从节点,属性,reg,ranges,中断控制器等几个方面叙述 ...

  8. websocket nova vnc proxy

    1. vnc proxy的实现原理 vnc 是nova提供的用来访问虚拟机的一项重要功能,用户可以通过websocket来访问,也可以通过java客户端来访问.通过websket访问虚拟机 的功能已经 ...

  9. Python模拟登录实战(三)

    目标:模拟登录知乎 代码如下: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'ziv·chan' import re impor ...

  10. Lucene和jackson冲突

    今天在使用lucene的时候,想直接在Controller中返回json对象,于是在Spring中配置了JackSon的converter: <bean id="jacksonMess ...