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(渲染回调模式)的更多相关文章

  1. JavaScript:回调模式(Callback Pattern) (转载)

    JavaScript:回调模式(Callback Pattern) 函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode() ...

  2. JavaScript学习笔记(十二) 回调模式(Callback Pattern)

    函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...

  3. JavaScript:回调模式(Callback Pattern)

    函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode(),然后在某个时间点,writeCode()有可能执行(调用)intr ...

  4. 腾讯优测优分享 | 探索react native首屏渲染最佳实践

    腾讯优测是专业的移动云测试平台,旗下的优分享不定时提供大量移动研发及测试相关的干货~ 此文主要与以下内容相关,希望对大家有帮助. react native给了我们使用javascript开发原生app ...

  5. 探索react native首屏渲染最佳实践

    文 / 腾讯 龚麒 0.前言 react native给了我们使用javascript开发原生app的能力,在使用react native完成兴趣部落安卓端发现tab改造后,我们开始对由react n ...

  6. React.render和reactDom.render的区别

    刚开始学习react.js.发现网上的资料,有些是写着react.render,有些写着reactDom.render.觉得很奇怪就查阅了一下资料.解释如下: 这个是react最新版api,也就是0. ...

  7. React Canvas:高性能渲染 React 组

    React Canvas 提供了使用 Canvas 渲染移动 Web App 界面的能力,替代传统的 DOM 渲染,具有更接近 Native App 的使用体验.React Canvas 提供了一组标 ...

  8. rpyc 回调模式工作不正常

    rpyc 回调模式工作不正常 最近使用了 rpyc 来处理一个多节点间的文件同步的任务,目标是使用 rpyc 来实现简单的 p2p 文件传输机制,以减少单点负载和单点失败对传输的影响. 和 p2p 的 ...

  9. ReactDom.render和React.render的区别

    这个是react最新版api,也就是0.14版本做出的改变.主要是为了使React能在更多的不同环境下更快.更容易构建.于是把react分成了react和react-dom两个部分.这样就为web版的 ...

随机推荐

  1. linux-centos6①

  2. poj3984迷宫问题(dfs+stack)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35426   Accepted: 20088 Descriptio ...

  3. Qt listwigwt item 加入自定义元素

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  4. C 计算员工工资

    #include <stdio.h> int main(int argc, char **argv) { //定义四个变量 g每小时固定的工资 40 固定工作时间 pay工资 hours员 ...

  5. chrome编辑器与截图

    在地址栏中输入 data:text/html,<html contenteditable>即可使用编辑功能,打开控制台,ctrl + shift + p 调用命令面板,输入 capture ...

  6. html常用小知识

    请求重定向:加载页面之后,除了用js做重定向之外,我们还可以直接用<meta>标签做重定向. <meta http-equiv="refresh" content ...

  7. Sharepoint 2013与Sharepoint 2016的功能对比

    开发人员功能 SharePoint Foundation 2013 SharePoint Server 2013 Standard CAL SharePoint Server 2013 Enterpr ...

  8. 最小生成树(II)与Kruskal算法

    为防止网页加载过慢,故分两章.上接https://www.cnblogs.com/Uninstalllingyi/p/10479470.html Kruskal算法——将森林合并成树 玩过瘟疫公司吗… ...

  9. UVa 455 - Periodic Strings - ( C++ ) - 解题报告

    1.题目大意 求一个长度不超过80的字符串的最小周期. 2.思路 非常简单,基本就是根据周期的定义做出来的,几乎不需要过脑. 3.应该注意的地方 (1) 最后输出的方式要注意,不然很容易就PE了.不过 ...

  10. 关于jquery几个自己不咋用到的常用遍历赛选的api

    1.contains:作用是返回包含某个文字的元素节点 例子:要给所以含有“lyz”的p节点加样式: 可以这样:$("p:contains(lyz)").css("col ...