redux与redux-react使用示例
redux使用
<script type="text/babel">
var Counter=React.createClass({ incrementIfOdd:function(){
if (this.props.value % 2 !== 0) {
this.props.onIncrement();
}
},
incrementAsync:function() {
setTimeout(this.props.onIncrement, 1000);
},
render:function() {
const { value, onIncrement, onDecrement } = this.props; return (
<p>
Clicked: {value} times
{' '}
<button onClick={onIncrement}>
+
</button>
{' '}
<button onClick={onDecrement}>
-
</button>
{' '}
<button onClick={this.incrementIfOdd}>
Increment if odd
</button>
{' '}
<button onClick={this.incrementAsync}>
Increment async
</button>
</p>
)
}
});
function counter(state, action) {
if (typeof state === 'undefined') {
return 0
} switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
} var store = Redux.createStore(counter)
function render(){
ReactDOM.render(
<div><Counter value={store.getState()} onIncrement={function(){store.dispatch({ type: 'INCREMENT' })}} onDecrement={function(){store.dispatch({ type: 'DECREMENT' })}}/></div>,
document.body
);
} $(document).ready(function(){
render();
store.subscribe(render);
});
</script>
redux使用
redux-react使用
<script type="text/babel">
var Counter=React.createClass({ incrementIfOdd:function(){
if (this.props.value % 2 !== 0) {
this.props.onIncrement();
}
},
incrementAsync:function() {
setTimeout(this.props.onIncrement, 1000);
},
render:function() {
const { value, onIncrement, onDecrement } = this.props; return (
<p>
Clicked: {value} times
{' '}
<button onClick={onIncrement}>
+
</button>
{' '}
<button onClick={onDecrement}>
-
</button>
{' '}
<button onClick={this.incrementIfOdd}>
Increment if odd
</button>
{' '}
<button onClick={this.incrementAsync}>
Increment async
</button>
</p>
)
}
}); function counter(state, action) {
if (typeof state === 'undefined') {
return 0
} switch (action.type) {
case 'INCREMENT': return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
} var store = Redux.createStore(counter)
function render(){
var TESTCounter=ReactRedux.connect(function(state, ownProps){
return {value:state}
},function(dispatch, ownProps){
return Redux.bindActionCreators({
onIncrement:function(){return { type: 'INCREMENT' }}
,
onDecrement:function(){
return { type: 'DECREMENT' };
}
},dispatch)
})(Counter); ReactDOM.render( <div><ReactRedux.Provider store={store}>
<TESTCounter />
</ReactRedux.Provider></div>,
document.body
);
} $(document).ready(function(){
render(); });
</script>
redux-react使用
记录以防忘记
redux与redux-react使用示例的更多相关文章
- 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)
一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...
- Redux:with React(一)
作者数次强调,redux和React没有关系(明明当初就是为了管理react的state才弄出来的吧),它可以和其他插件如 Angular, Ember, jQuery一起使用.好啦好啦知道啦.Red ...
- React - redux, jsx中写js示例
{ this.state.avatarSource === null ? <Text>Select a Photo</Text> : <Image style={styl ...
- [转] What is the point of redux when using react?
As I am sure you have heard a bunch of times, by now, React is the V in MVC. I think you can think o ...
- [Redux] Filtering Redux State with React Router Params
We will learn how adding React Router shifts the balance of responsibilities, and how the components ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- 25.redux回顾,redux中的action函数异步
回顾:Redux: 类似于 Vuex 概念:store/reducer/action action:动作 {type,.....} 一定要有type 其他属性不做限制 reducer:通过计算产生st ...
- React-安装和配置redux调试工具Redux DevTools
chrome扩展程序里搜索Redux DevTools进行安装 新建store的时候,进行如下配置. import { createStore, applyMiddleware ,compose} f ...
- Redux 和 Redux thunk 理解
1: state 就像 model { todos: [{ text: 'Eat food', completed: true }, { text: 'Exercise', completed: fa ...
- [Redux] Understand Redux Higher Order Reducers
Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...
随机推荐
- win7使用问题解决
1. VM和主机互相PING不通 问题:桥接模式,VM可以ping 通外网,可以ping 通局域网其它机子,就是ping 不通本地主机 解决:将 vm网卡和本地网连接网卡都共享出来
- 02:MongoDB操作
1.1 MongoDB 增加 1.插入数据 1. 插入文档: insert 如果插入数据的时候,collection还不存在,自动创建集合 2. insertOne: 插入一条数据 3. insert ...
- Ymodem协议(参考STM32)
相信很多人都希望,不开盖就可以对固件进行升级吧,就像手机那些.下文中的bootload就来实现这样的功能. 前段时间有项目关于Bootload设计.所以就仔细的去了研究了一翻.以前都是用的stm32官 ...
- linux下如何调试Makefile?
答:make --debug=all (输出所有的调试信息)
- bzoj1565: [NOI2009]植物大战僵尸 最大权闭合子图,tarjan
bzoj1565: [NOI2009]植物大战僵尸 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1565 思路 很容易的想到最大权闭合子图 ...
- centos6.5下安装mysql5.6
链接: https://blog.csdn.net/liangzhuoxun/article/details/81572094 该链接有个错误: 让修改的profile文件立即生效的命令为./etc/ ...
- 用C#.NET调用Java开发的WebService传递int,double问题,出现java无法获得值!
https://www.cnblogs.com/zhbsh/archive/2013/04/22/3035477.html 用C#.NET调用Java开发的WebService时,先在客户端封装的带有 ...
- 【Hadoop 分布式部署 四:配置Hadoop 2.x 中主节点(NN和RM)到从节点的SSH无密码登录】
******************* 一定要使这三台机器的用户名相同,安装目录相同 ************* SSH 无密钥登录的简单介绍(之前再搭 ...
- vue.js精讲01
笔记及源码地址 : https://github.com/wll8/vue_note 01 2017-09-13 view一个 mvvm框架(库),和 ag 类似.比较小巧,容易上手. mvc: mv ...
- Windows下使用命令安装Python的scipy库出错的解决
平时使用Python都是在Sublime下使用,不想使用IDE.使用各种库时安装也就是使用pip安装即可.来说说今天自己遇到的一个问题:使用scipy数学库时,使用命令: pip install sc ...