When making a reusable component, you'll find that people often like to have the API they're most familiar with, so in this lesson we'll recreate the withToggle Higher Order Component using our new ConnectedToggle render prop component.

function withToggle(Component) {
function Wrapper(props, context) {
const {innerRef, ...remainingProps} = props
return (
<ConnectedToggle
render={toggle => (
<Component
{...remainingProps}
toggle={toggle}
ref={innerRef}
/>
)}
/>
)
}
Wrapper.displayName = `withToggle(${Component.displayName ||
Component.name})`
Wrapper.propTypes = {innerRef: PropTypes.func}
Wrapper.WrappedComponent = Component
return hoistNonReactStatics(Wrapper, Component)
}

[React] Implement a Higher Order Component with Render Props的更多相关文章

  1. [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

    In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that ...

  2. [React Intl] Use a react-intl Higher Order Component to format messages

    In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...

  3. [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props

    Because @types/react has to expose all its internal types, there can be a lot of confusion over how ...

  4. 可复用 React 的 HOC 以及的 Render Props

    重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品(领导)打脸,真的最后一次改需求,我们烦恼于频繁修改的需求 虽然我们不能改变别人,但我们却可以尝试去做的更好,我们需要抽象,封装 ...

  5. [React] Use React.memo with a Function Component to get PureComponent Behavior

    A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...

  6. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  7. [React] Implement a React Context Provider

    If you have state that needs to exist throughout your application, then you may find yourself passin ...

  8. [React] Creating a Stateless Functional Component

    Most of the components that you write will be stateless, meaning that they take in props and return ...

  9. React Render Props 模式

    概述 Render Props模式是一种非常灵活复用性非常高的模式,它可以把特定行为或功能封装成一个组件,提供给其他组件使用让其他组件拥有这样的能力,接下来我们一步一步来看React组件中如何实现这样 ...

随机推荐

  1. http_build_query 字符串拼接

    http_build_query 字符串拼接 产生一个urlencode之后的请求字符串. 1.将数组转化成url问号(?)后的字符串 <?php $date=array( 'name'=> ...

  2. Matlab---从入门到精通 Chapter 4 编程基础

    ---恢复内容开始--- 4-1 M文件编辑器 在命令窗口输入edit命令,可以打开M文件编辑器,创建新的M文件 在命令行中输入edit filename,那么可以打开在当前目录环境下的M文件 4-2 ...

  3. c#获取DataTable某一列不重复的值,或者获取某一列的所有值

    实现该功能是用了DataView的筛选功能,DataView表示用于排序.筛选.搜索.编辑和导航的 DataTable 的可绑定数据的自定义视图. 这里做了一个简单易懂的Demo来讲述该方法. 1.建 ...

  4. UI控件设置

    去掉cesium默认的版权信息: 在style样式中添加 .cesium-widget-credits {display:none !important} 去掉动画控件.地址搜索控件.图层选择控件.操 ...

  5. Eclipse配置class文件输出目录

    1, Eclipse选中项目名称,邮件选中“Build Path”,然后选择“Configure Build Path”--->选择“Source” Tab---->修改"Def ...

  6. 【codeforces 496E】Distributing Parts

    [题目链接]:http://codeforces.com/contest/496/problem/E [题意] 给你n个歌曲; 每个歌曲有一个需要声音的区间li,ri; 然后给你m个人; 每个人也有一 ...

  7. Obfuscating computer code to prevent an attack

    A method and system for obfuscating computer code of a program to protect it from the adverse effect ...

  8. 不安全的直接对象引用:你的 ASP.NET 应用数据是否安全?

    介绍 作为一个在X94的航空工程师,你的老板要求你从2号楼的工程图中检索出一个特定的专利.不幸的是,进入大楼需要你出示你具有进入大楼的资格的证明,然后你迅速地以徽章的形式出示给了保安.到了十三楼,进入 ...

  9. hdu5105Math Problem(分类讨论)

    题目链接: huangjing 题目: 思路: 给出的是一个方程,首先讨论最高项系数. 1:a==0&& b==0  那么函数就是线性的.直接比較端点就可以. 2   a==0& ...

  10. Hello World FastCGI

    什么是FastCGI,google吧,測试一个用C++实现的FastCGI程序. 1, Nginx 安装.http://nginx.org/en/download.html.下载解压.configur ...