1、package.json:

{
"name": "wtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"build": "NODE_ENV=production&&npm run output",
"output": "webpack --config webpack.build.js",
"test": "node ./dist/test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.22.0",
"babel-preset-es2016": "^6.22.0",
"babel-preset-es2017": "^6.22.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-0": "^6.22.0",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.26.2",
"extract-text-webpack-plugin": "^2.0.0",
"file-loader": "^0.10.1",
"html-webpack-plugin": "^2.28.0",
"mobx": "^3.1.2",
"mobx-react": "^4.1.1",
"node-sass": "^4.5.0",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-hot-loader": "^1.3.1",
"react-router": "^3.0.2",
"reload": "^1.1.1",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"uglifyjs-webpack-plugin": "^0.3.0",
"webpack": "^2.2.1",
"webpack-del-plugin": "^0.0.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.1",
"webpack-hot-middleware": "^2.17.1",
"webpack-spritesmith": "^0.3.1",
"webpack-uglify-js-plugin": "^1.1.9"
}
}

2、webpack:

const webpack = require('webpack');
const webpackUglifyJsPlugin = require('webpack-uglify-js-plugin');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin'); const config = {
entry: [
"webpack-hot-middleware/client?reload=true",
// 这里是你的入口文件
"./index.js",
],
output: { //输出目录
publicPath: "",
path: __dirname,
filename: 'bundle.js',
},
module: {
rules: [{
test: /\.jsx?$/,
use: ['react-hot-loader', {
loader: 'babel-loader',
options: {
presets: ['es2015', 'es2016', 'es2017', 'stage-0', 'react'], //babel presets ,首先需要react解析react然后再es语法编译成js
plugins: ['transform-decorators-legacy']
}
}],
exclude: /node_modules/
}, {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
loader: "css-loader!autoprefixer-loader?{browsers:['last 6 Chrome versions', 'last 3 Safari versions', 'iOS >= 5', 'Android >= 4.0']}!sass-loader",
}),
}, {
test: /\.png$/,
use: {
loader: 'file-loader',
options: {
name: '../img/[name].[ext]'
}
}
}]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(), //热编译开启
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('css/style.css')
/*new HtmlWebpackPlugin({
title: 'index',
hash:true,
template: 'index.ejs', // Load a custom template (ejs by default see the FAQ for details)
})*/
]
};
module.exports = config;

3、server.js:

var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
config = require("./webpack.start.js"),
express = require('express'),
app = express(),
compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
noInfo: true,
stats: {
colors: true,
progress: true
}
}));
app.use(webpackHotMiddleware(compiler, { //中间件实现热刷新
noInfo: true,
publicPath: config.output.publicPath
})); app.get('*', function(req, res) {
var fileName = req.url;
console.log(fileName);
if (fileName == '/') {
res.sendFile(__dirname + '/index.html');
}else{
res.sendFile(__dirname + fileName);
}
});
app.listen(8087,'0.0.0.0'); //node 构建server

3、store:

import { observer } from "mobx-react";
import { observable, action, computed ,autorun} from "mobx";
export default class NewStore {
@observable inputValue1; //注册监听的数据
@observable inputValue2;
@observable childValue;
constructor() {
this.inputValue1=0;
this.inputValue2=0;//初始化数据的值
this.fullValue=0;
this.childValue=0;
}
@action changeValue(s,e){ //当促发action的时候,改变对应的数据
let tar=e.currentTarget;
let val=Math.abs(tar.value);
if(tar.name=='val1'){
this.inputValue1=val;
}else if(tar.name=='val2'){
this.inputValue2=val;
}
} @computed get sum(){ //computed 是自动监听登记的数据,如果对应数据有改变就自动执行该函数,然后返回数据给接收的接口
this.fullValue=this.inputValue1+this.inputValue2;
console.log(this.fullValue)
return this.fullValue;
}
@action childEvent(){
this.childValue=this.childValue+3;
}
}
import TestStore from  './test.js';
import NextStore from "./next.js";
import NewStore from "./new.js";
import FormStore from "./form.js";
import MenuStore from "./menu.js";
import NumChange from "./comm/numChange.js" export default {
testStore:new TestStore(),
nextStore:new NextStore(),
newStore:new NewStore(),
formStore:new FormStore(),
menuStore:new MenuStore(),
numChange:new NumChange()
}

4、index.js:

import "./js/auto_rem.js";
import "./css/style.scss";
import React from "react";
import { render } from "react-dom";
import { observable, computed, autorun } from "mobx";
import { Provider } from 'mobx-react';
import { Router, Route, hashHistory ,browserHistory} from 'react-router';
import Test from "./src/components/test.js";
import Next from "./src/components/next.js";
import New from "./src/components/new.js";
import Forms from "./src/components/form.js";
import Menu from "./src/components/menu.js";
import store from "./src/store/store.js"; const routes = (
<Route component={App}>
<Route path="/" component={Menu}/>
<Route path="/menu" component={Menu}/>
<Route path="/form" component={Forms}/>
<Route path="/new" component={New}/>
<Route path="/test" component={Test}/>
<Route path="/next" component={Next}/>
</Route>
);
class App extends React.Component {
render() {
return (
<Provider {...store}> //注入所有的store
<Router history={hashHistory} >
{routes}
</Router>
</Provider>
)
}
} render( < App />, document.getElementById('app'));

  

4、view:

import {observer,inject} from "mobx-react";
import {observable,action,computed,autorun} from "mobx"; import React,{Component} from "react";
import {render} from "react-dom";
import Child from "./comm/child.js";
@inject(['newStore']) @observer //引入需要的store,然后组件就可以在props中访问
export default class New extends Component{
constructor(props) {
super(props);
this.store=this.props.newStore; //访问注入的store
this.changeValue=this.store.changeValue.bind(this.store,event) //访问mobx中的事件,store中的事件
}
render(){
return(
<div>
<input type='tel' name='val1' onKeyUp={this.changeValue}/>+
<input type='tel' name='val2' onKeyUp={this.changeValue}/>=
<span>{this.store.sum}</span>
<Child/>
</div>
)
}
}

  

react mobx webpack 使用案例的更多相关文章

  1. React + MobX 状态管理入门及实例

    前言 现在最热门的前端框架,毫无疑问是React. React是一个状态机,由开始的初始状态,通过与用户的互动,导致状态变化,从而重新渲染UI. 对于小型应用,引入状态管理库是"奢侈的&qu ...

  2. 前端003/【React + Mobx + NornJ】开发模式

    1.React + Mobx + NornJ 开发模式快速上手教程 github网址:https://github.com/joe-sky/nornj-cli/blob/master/docs/gui ...

  3. react+react-router+webpack+express+nodejs

    react+react-router+webpack+express+nodejs   做SinglePageApplication 支持热加载+ES6 有开发模式和发布模式 https://gith ...

  4. 入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 说说React ...

  5. reactjs学习一(环境搭配react+es6+webpack热部署)

    reactjs学习一(环境搭配react+es6+webpack热部署) 本文的源码在这里下载 https://github.com/tianxiangbing/webpack-study   或者使 ...

  6. spring + spring mvc + mybatis + react + reflux + webpack Web工程例子

    前言 最近写了个Java Web工程demo,使用maven构建: 后端使用spring + spring mvc + mybatis: 前端使用react + react-router+ webpa ...

  7. 轻松入门React和Webpack

    最近在学习React.js,之前都是直接用最原生的方式去写React代码,发现组织起来特别麻烦,之前听人说用Webpack组织React组件得心应手,就花了点时间学习了一下,收获颇丰 <!-- ...

  8. 在React+Babel+Webpack环境中使用ESLint

    ESLint是js中目前比较流行的插件化的静态代码检测工具.通过使用它可以保证高质量的代码,尽量减少和提早发现一些错误.使用eslint可以在工程中保证一致的代码风格,特别是当工程变得越来越大.越来越 ...

  9. React+ES6+Webpack环境配置

    转自http://www.cnblogs.com/chenziyu-blog/p/5675086.html 参考http://www.tuicool.com/articles/BrAVv2y Reac ...

随机推荐

  1. mina基础知识整理

    一.      简介: Apache Mina Server 是一个网络通信应用框架,Mina 可以帮助我们快速开发高性能.高扩展性的网络通信应用,Mina 提供了事件驱动.异步(Mina 的异步 I ...

  2. AI update

    1, labeling工具 - 测试完成 使用fiji + Alps_Labeling_Tool.ijm 做labeling 生成的文件可以使用python读取 2,training -未开始 使用t ...

  3. php调用c#的dll(转)

    这几天,一直在做DES ecb模式的加解密,刚用.net实现了加解密,完了由于需要又要转型成PHP代码,费了九牛二虎之力单独用PHP没能实现,结构看到一篇php直接调用c#里生成的.dll文件的方法, ...

  4. NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

    组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...

  5. hanlp中文智能分词自动识别文字提取实例

    需求:客户给销售员自己的个人信息,销售帮助客户下单,此过程需要销售人员手动复制粘贴收获地址,电话,姓名等等,一个智能的分词系统可以让销售人员一键识别以上各种信息 经过调研,找到了一下开源项目 1.wo ...

  6. flume 架构设计优化

    对于企业中常用的flume type 概括如下:ource(获取数据源): exec (文件) spoolingdir (文件夹) taildir(文件夹及文件的变动) kafka syslog ht ...

  7. [转CSDN多篇文章]WEB 3D SVG CAD 矢量 几种实现方案

    WEB 3D SVG CAD 矢量 几种实现方案 原创 2014年10月24日 08:34:11 标签: WEB3D / CADSVG / 矢量 2665 一.全部自己开发,从底层开始 VML+SVG ...

  8. 解决Windows远程桌面连接每次都提示输入密码的问题,远程桌面记不住密码

    FROM:http://www.veryhuo.com/a/view/80444.html Windows 远程桌面连接几乎每天都用,所以使用的方便性非常重要.如果你经常用,也许会发现在某些系统中,每 ...

  9. webGL之three.js入门2

    入门建议: webGL中文翻译教程,基于NeHe的openGL教程:http://www.hiwebgl.com/?p=42 . WebGL中文网 http://www.hewebgl.com/ ,里 ...

  10. 用winrar和ftp命令实现自动备份文件并自动上传到指定的ftp服务器

    这篇文章主要介绍了用winrar和ftp命令实现自动备份文件并自动上传到指定的ftp服务器的方法,需要的朋友可以参考下. http://www.jb51.net/article/50359.htm 1 ...