React Render Callback Pattern(渲染回调模式)
React Render Callback Pattern,渲染回调模式,其实是将this.props.children当做函数来调用。
例如:
要根据user参数确定渲染Loading还是Profile组件
const App = () => {
return (
<div>
<FieldItem username='magalhini'>
{user => user === null
? <Loading />
: <Profile info={user} />}
</FieldItem>
</div>
);
};
Render Callback Pattern的做法是:
class FieldItem extends React.Component {
state = { user: null }
componentDidMount() {
// We can make an ajax call here, for e.g.
setTimeout(() => this.setState({
user: `I have now fulfilled something for ${this.props.username}`
}), 1500);
}
render() {
// Render the children with a function using state as the argument
return this.props.children(this.state.user);
}
}
关键是这句 this.props.children(this.state.user),将this.props.chilren当做函数,将user作为参数传入。
完整代码如下:
// Loading component
const Loading = () => <p>Loading...</p>; // Profile component
const Profile = (props) => <p>{props.info}</p>; const App = () => {
return (
<div>
<h3>An application</h3>
<FieldItem username='magalhini'>
{user => user === null
? <Loading />
: <Profile info={user} />}
</FieldItem>
</div>
);
}; class FieldItem extends React.Component {
state = { user: null } componentDidMount() {
// We can make an ajax call here, for e.g.
setTimeout(() => this.setState({
user: `I have now fulfilled something for ${this.props.username}`
}), 1500);
}
render() {
// Render the children with a function using state as the argument
return this.props.children(this.state.user);
}
} ReactDOM.render(<App/>, document.getElementById('app'));
出处:http://blog.ricardofilipe.com/post/react-callback-render-pattern
React Render Callback Pattern(渲染回调模式)的更多相关文章
- JavaScript:回调模式(Callback Pattern) (转载)
JavaScript:回调模式(Callback Pattern) 函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode() ...
- JavaScript学习笔记(十二) 回调模式(Callback Pattern)
函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...
- JavaScript:回调模式(Callback Pattern)
函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...
- 腾讯优测优分享 | 探索react native首屏渲染最佳实践
腾讯优测是专业的移动云测试平台,旗下的优分享不定时提供大量移动研发及测试相关的干货~ 此文主要与以下内容相关,希望对大家有帮助. react native给了我们使用javascript开发原生app ...
- 探索react native首屏渲染最佳实践
文 / 腾讯 龚麒 0.前言 react native给了我们使用javascript开发原生app的能力,在使用react native完成兴趣部落安卓端发现tab改造后,我们开始对由react n ...
- React.render和reactDom.render的区别
刚开始学习react.js.发现网上的资料,有些是写着react.render,有些写着reactDom.render.觉得很奇怪就查阅了一下资料.解释如下: 这个是react最新版api,也就是0. ...
- React Canvas:高性能渲染 React 组
React Canvas 提供了使用 Canvas 渲染移动 Web App 界面的能力,替代传统的 DOM 渲染,具有更接近 Native App 的使用体验.React Canvas 提供了一组标 ...
- rpyc 回调模式工作不正常
rpyc 回调模式工作不正常 最近使用了 rpyc 来处理一个多节点间的文件同步的任务,目标是使用 rpyc 来实现简单的 p2p 文件传输机制,以减少单点负载和单点失败对传输的影响. 和 p2p 的 ...
- ReactDom.render和React.render的区别
这个是react最新版api,也就是0.14版本做出的改变.主要是为了使React能在更多的不同环境下更快.更容易构建.于是把react分成了react和react-dom两个部分.这样就为web版的 ...
随机推荐
- cf#516A. Make a triangle!(三角形)
http://codeforces.com/contest/1064/problem/A 题意:给出三角形的三条边,问要让他组成三角形需要增加多少长度 题解:规律:如果给出的三条边不能组成三角形,那答 ...
- VS2015 更改C++模式
亲爱的小伙伴,有没有发现你们的VS2015装完以后和老江湖们用的不一样了,人家的界面打开是这样的 而你的界面打开是这样的 虽然看是只有一左一右的区别,但是内在确实有好多不一样. 想不想想老江湖一样,拥 ...
- 第三模块:面向对象&网络编程基础 第3章 选课系统作业讲解
01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课系统作业讲解4 01-选课系统作业讲解1 02--选课系统作业讲解2 03-选课系统作业讲解3 04--选课 ...
- Struts2(十.在修改页显示照片列表并增加删除照片功能)
一.显示照片列表功能 struts2中一般的处理方式:先在action中,准备数据,转到jsp中显示 1.UserAction /** * 点击修改用户按钮跳转到修改用户界面 * 为用户准备照片,以便 ...
- lintcode413 反转整数
反转整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 您在真实的面试中是否遇到过这个题? Yes 样例 给定 x = 123,返回 321 给定 x = ...
- 孤荷凌寒自学python第八十一天学习爬取图片1
孤荷凌寒自学python第八十一天学习爬取图片1 (完整学习过程屏幕记录视频地址在文末) 通过前面十天的学习,我已经基本了解了通过requests模块来与网站服务器进行交互的方法,也知道了Beauti ...
- MySQL数据库怎么截取字符串?
函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...
- python图片大小处理;
循环一个目录将下面的所有png或者jpg文件全部缩小一定比例: from PIL import Image import os,re work_dir = 'C:\\Users\\Admini ...
- EF中如何为表添加新的字段和映射
首先先了解一下ef生成的模型edmx的代码,传送门:http://www.cnblogs.com/yushengbo/p/4807715.html 一.添加新的字段 例子就用我现在项目的这个吧,首先在 ...
- [转载]Tensorflow中reduction_indices 的用法
Tensorflow中reduction_indices 的用法 默认时None 压缩成一维