- 内容简介
- 译者序
- 前言
- 第 1 章 安装配置新项目
- 第 2 章 Flexbox 布局介绍
- 第 3 章 用 React Native 开发一个应用
- 第 4 章 在 React Native 中使用导航
- 第 5 章 动画和滑动菜单
- 第 6 章 用 React Native 绘制 Canvas
- 第 7 章 使用 React Native 播放音频
- 第 8 章 你的第一个自定义视图
- 第 9 章 Flux 介绍
- 第 10 章 处理复杂的应用程序状态
- 第 11 章 使用 Node 来实现服务端 API
- 第 12 章 在 React Native 中使用文件上传
- 第 13 章 理解 JavaScript Promise
- 第 14 章 fetch 简介
- 第 15 章 在 iOS 中使用 SQLite
- 第 16 章 集成 Google Admob
- 第 17 章 React Native 组件国际化
- 附录 A React.js 快速介绍
- 附录 B Objective-C Primer
- 附录 C webpack 入门
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
把状态树并入 ReactJS
我们已经深入了解了 Baobab 是怎样工作的。但是,Baobab 的真正强大之处在于如何与 ReactJS 整合。让我们首先看一下如何把状态树和 React JS 组件结合到一起,从而可以响应树上的事件来获取数据改变。然后,我会解释为什么指定组件只去关注感兴趣的状态改变,而不是监听所有事件。
连接状态树和 React 组件有两种策略。下面我们就使用前面定义的状态树来实践一下。
将单个游标连接到组件
var stateTree = require('./stateTree.js');
var React = require('react');
var listCursor = stateTree.select('admin', 'notifications', 'list')
;
var myComponent = React.createClass({
mixins: [listCursor.mixin],
renderNotification: function(notification){
return (
<li>{notification.title}</li>
);
},
render: function(){
return (
<ul>
{this.state.cursor.map(this.renderNotification)}
</ul>
);
}
});
将多个游标连接到组件
var stateTree = require('./stateTree.js');
var React = require('react');
var listCursor = stateTree.select('admin', 'notifications', 'list')
;
var myComponent = React.createClass({
mixins: [listCursor.mixin],
cursors: {
notifications: ['admin', 'notifications', 'list'],
feeds: ['home', 'feeds']
},
renderFeed: function(feed) {
return (
<li>{feed.title}</li>
);
},
render: function(){
return (
<div>
<div>You have {this.state.cursors.notifications.length}
notifications</div>
<ul>
{this.state.cursors.feeds.map(this.renderFeeds)}
</ul>
</div>
);
}
});
我们已经看到将 state 连接到一个组件是多么简单。如果你想创建多个游标,可以引入一个树的全量,但是依然可以只引入一个游标用来抓取状态。当这些游标的数据发生了改变时,组件就会自动渲染,并且新的值可以通过组件的 state 对象的 cursors 或者 cursor 属性获取。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论