Because @types/react has to expose all its internal types, there can be a lot of confusion over how to type specific patterns, particularly around higher order components and render prop patterns. The widest and most recommended element type is React.ReactNode, and we build up to it by explaining the edge cases.

For render prop:

type ButtonProps = {
label?: string;
children: (b: boolean) => React.ReactNode;
};
function App() {
return (
<Button>
{isOn => (isOn ? <div> Turn off</div> : <div> Turn on</div>)}
</Button>
);
}
type ButtonProps = {
label?: string;
children: React.ReactNode;
};
type ButtonState = {
isOn: boolean;
};
class Button extends React.Component<ButtonProps, ButtonState> {
static defaultProps = {
label: "Hello World!"
};
state = {
isOn: false
}; toggle = () => this.setState({ isOn: !this.state.isOn }); render() {
const { label, children } = this.props;
const { isOn } = this.state;
const style = {
backgroundColor: isOn ? "red" : "green"
};
return (
<button style={style} onClick={this.toggle}>
{children(isOn)}
</button>
);
}
}

For React children projection:

type ButtonProps = {
label?: string;
children: React.ReactNode;
};

[React] Use React.ReactNode for the children prop in React TypeScript components and Render Props的更多相关文章

  1. [React] Use Prop Collections with Render Props

    Sometimes you have common use cases that require common props to be applied to certain elements. You ...

  2. [React] Use React.cloneElement to Modify and Add Additional Properties to React Children

    In this lesson we'll show how to use React.cloneElement to add additional properties to the children ...

  3. [React] Use React.cloneElement to Extend Functionality of Children Components

    We can utilize React.cloneElement in order to create new components with extended data or functional ...

  4. React Render Props 模式

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

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

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

  6. React 之 render props 的理解

    1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...

  7. React中render Props模式

    React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...

  8. 关于React.PropTypes的废除,以及新版本下的react的验证方式

    React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...

  9. React高阶组件 和 Render Props

    高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...

随机推荐

  1. Webpack 性能优化 (一)(使用别名做重定向)

    前言 Webpack 是 OneAPM 前端技术栈中非常重要的一部分.它非常好用,假设你还不了解它,建议你阅读这篇Webpack 入门指迷,在 OneAPM 我们用它完毕静态资源打包.ES6 代码的转 ...

  2. systemtap 调试postgrel

    http://blog.163.com/digoal@126/blog/static/16387704020137140265557/   dtrace http://blog.163.com/dig ...

  3. WinForm MDIParent如何防止重复打开

    DI,全称是多文档界面(Multiple Document Interface),主要应用于基于图形用户界面的系统中.其目的是同时打开和显示多个文档,便于参考和编辑资料. 下面是一个WinForm M ...

  4. hdu 4865 Peter&#39;s Hobby

    Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  5. Modbus读写模拟量寄存器具体解释

    读可读写模拟量寄存器: 发送命令(主机向从机)格式: [设备地址] [命令号03] [起始寄存器地址高8位] [低8位] [读取的寄存器数高8位] [低8位] [CRC校验的低8位] [CRC校验的高 ...

  6. IIS7下配置ASP+ACCESS环境

    先要设置应用程序池(Application Pool)为Classic .NET AppPool,而不是默认的Default AppPool,可以在网站目录里对每个站点设置,也可以在站点进行单独设置. ...

  7. xcode 不能选择模拟器

    重新安装了xcode后,程序无法运行,不能选择模拟器 或者 设备? 1.你之所以选择不了模拟器或者设备,是因为你工程中的iOS Deployment Target设置不对.比如你装的是xcode4.0 ...

  8. C#实现缩放字体

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. Library drmframework_jni not found

    http://piotrbuda.eu/2012/06/trying-to-solve-error-491-in-play-store-on-android-emulator.html http:// ...

  10. 好用的批量改名工具——文件批量改名工具V2.0 绿色版

    我找了一个绿色免安装的软件来实现批量改名要求 下载地址:http://www.orsoon.com/Soft/14049.html#xiazai 添加图片后,开始改名.通过输入a#就可以将这些图片进行 ...