本教程总共5篇,每日更新一篇,请关注我们!你可以进入历史消息查看以往文章,也敬请期待我们的新文章!
1、React第三方组件4(状态管理之Reflux的使用①简单使用)---2018.03.13
2、React第三方组件4(状态管理之Reflux的使用②TodoList上)---2018.03.14
3、React第三方组件4(状态管理之Reflux的使用③TodoList中)---2018.03.15
4、React第三方组件4(状态管理之Reflux的使用④TodoList下)---2018.03.16
5、React第三方组件4(状态管理之Reflux的使用⑤异步操作)---2018.03.19
开发环境:Windows 8,node v8.9.1,npm 5.5.1,WebStorm 2017.2.2
先说下ReFlux
引用:https://segmentfault.com/a/1190000004843954
一个简单的单向数据流应用库,灵感来自于ReactJS Flux.
╔═════════╗ ╔════════╗ ╔═════════════════╗
║ Actions ║──>║ Stores ║──>║ View Components ║
╚═════════╝ ╚════════╝ ╚═════════════════╝
^ │
└──────────────────────────────────────┘
同React Flux比较
refluxjs的目标是为了让我们更容易的搭建Web应用程序。
相同点
1、有actions
2、有stores
3、单向数据流
不同点
1、通过内部拓展actions的行为,移除了单例的dispatcher
2、stores可以监听actions的行为,无需进行冗杂的switch判断
3、stores可以相互监听,可以进行进一步的数据聚合操作,类似于,map/reduce
4、waitFor被连续和平行的数据流所替代
我们直接撸码!
先安装reflux
npmi-S reflux
1、我们建立下reflux目录,及reflux1目录,和Index.jsx
2、reflux下的Index.jsx代码
importReactfrom'react';
import{HashRouter,Route,NavLink,Redirect}from'react-router-dom';
importReFlux1from'./reFlux1/Index'
constIndex= () =>
ReFlux1
render={() => ()}/>
;
export defaultIndex;
3、reflux1下建立Index.jsx
importReactfrom'react'
importRefluxfrom'reflux'
importActionfrom'./Action'
importStorefrom'./Store'
classIndexextendsReflux.Component {
constructor(props) {
super(props);
this.store= Store;
}
render() {
return(
{this.state.num}
Action.add()}>+
);
}
}
export defaultIndex;
注意这几个框起来的!
4、建立Store.js
importRefluxfrom'reflux'
importActionfrom'./Action'
classStoreextendsReflux.Store {
constructor() {
super();
this.listenables= Action;
this.state= {
num:
}
}
onAdd() {
this.setState({num:this.state.num+1});
}
}
export defaultStore;
5、建立Action.js
importRefluxfrom'reflux'
letAction = Reflux.createActions([
'add'
]);
export defaultAction;
到这里就结束了,相对比较简单!
6、查看浏览器
本文完
禁止擅自转载,如需转载请在公众号中留言联系我们!
感谢童鞋们支持!
如果你有什么问题,可以在下方留言给我们!
领取专属 10元无门槛券
私享最新 技术干货