mobx react
目录结构:

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的更多相关文章
- MobX+react使用小demo
第一次接触mobx,网上找了很多例子,写此主要总结一下create-react-app + mobx入门 create-react-app myreact cd myreact npm install ...
- [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 ...
- mobx在react的使用
创建项目第六步 mobx 1.安装 yarn add mobx yarn add mobx-react 2.新建/src/store/store.js import {observable, co ...
- React MobX 开始
MobX 用于状态管理,简单高效.本文将于 React 上介绍如何开始,包括了: 了解 MobX 概念 从零准备 React 应用 MobX React.FC 写法 MobX React.Compon ...
- react学习一篇就够了
webstrom自动格式化代码 命令 js框架 MVC 安装 npm install create-react-app -g 生成项目(项目名npm发包包命名规范 /^[a-z0-9_-]$/) cr ...
- [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 ...
- Mobx-React : 当前适合React的状态管理工具
MobX 简单.可扩展的状态管理 MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商所赞助的. 安装 安装: npm install mobx ...
- React的状态管理工具
Mobx-React : 当前最适合React的状态管理工具 MobX 简单.可扩展的状态管理 MobX 是由 Mendix.Coinbase.Facebook 开源和众多个人赞助商 ...
- 【译】Redux 还是 Mobx,让我来解决你的困惑!
原文地址:Redux or MobX: An attempt to dissolve the Confusion 原文作者:rwieruch 我在去年大量的使用了 Redux,但我最近都在使用 Mob ...
随机推荐
- 安卓使用Dialog创建普通对话框
Activity页面简单所以XML不再写出.下面给出核心代码: button1=(Button)findViewById(R.id.button1); //为按钮设置监听器 button1.setO ...
- Install RHadoop with Hadoop 2.2 – Red Hat Linux
Prerequisite Hadoop 2.2 has been installed (and the below installation steps should be applied on ea ...
- MYSQL主从同步测试
参考: http://www.cnblogs.com/zgx/archive/2011/09/13/2174823.html 注意选建同步用户,其它的都按步就搬. 还有,不要让IPTABLES坏事,开 ...
- Zend framework重定向的方法
zend framework重定向的方法有三种.render, forward, redirect.它们各自的用法是什么样子的呢?有什么区别呢? 一.render render是用来调视图用的,不会调 ...
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
- 设计模式(二): BUILDER生成器模式 -- 创建型模式
1.定义 将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式. 2.适用场景 1. 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式 ...
- Linux环境下使用图形化界面的SVN客户端软件-RabbitVCS
如果想在Linux环境下使用图形化界面的SVN客户端软件,那么RabbitVCS绝对是首选,可以媲美Windows环境下用的TortoiseSVN,甚至连操作都基本一样,所以强烈推荐给各位童鞋. Ra ...
- 解决Chrome无法加载Shockwave Flash
Shockwave Flash 是 Adobe Flash Player下的一个小插件,你可以在Google商店中找到并下载. 通常来讲,Shckwave Flash会在安装Flash Player的 ...
- ubuntu14.04下交叉编译器的安装
今天打算换个工作环境,在ubuntu下装交叉编译器,可谓一波三折.最后总算是装好了. 首先参照一下这位仁兄的博客http://blog.csdn.net/silleyj/article/details ...
- cURL.io - Share your files right from your terminal
cURL.io - Share your files right from your terminal cURL.io