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. mongodb chunk 大小设置

    默认是64MB,取值范围是1 MB 到 1024 MB. 那改动会造成什么?下表简单总结: chunk size 调节 splitting次数(碎片数) 数据跨shard数目 数据均匀 网络传输次数 ...

  2. systemtap 安装 总结

    http://blog.soul11201.com/notes/2017/02/22/systemstap-install.html

  3. ubuntu的配置文件

    ubuntu的配置文件 是 ~/.gconf 我是把终端弄挂了, 只能再桌面系统下找到 ~/.gconf 下的相应文件 修改后就恢复到原来状态.

  4. 前端使用AngularJS的$resource,后端ASP.NET Web API,实现分页、过滤

    在上一篇中实现了增删改查,本篇实现分页和过滤. 本系列包括: 1.前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查2.前端使用AngularJS的$re ...

  5. Bootstrap碎语

    这里记录下某段时间Bootstrap的零散碎片. 1.有关Bootstrap的参考网站: ● 官方:http://getbootstrap.com/● 主题:http://bootswatch.com ...

  6. IOS NSLog 打印bool值

    输出BOOL值的方法:NSLog(@"%@",YES?@"YES":@"NO");%@输出字符串. NSLog(@"ifReadO ...

  7. java collection.frequency方法

    collection.frequency方法,可以统计出某个对象在collection中出现的次数 比如: frequency(Collection<?> c, Object o)     ...

  8. UITextField的简易封装

    UITextField的简易封装 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 UITextFieldView // ...

  9. java数组和字符串相互转换

    将字符串变成数组 Java.lang包中有String.split()方法,java中通常用split()分割字符串,返回的是一个数组. 特殊,转义字符,必须加"\\"(“.”和“ ...

  10. hihocoder #1170 机器人 &amp;&amp; 编程之美2015复赛

    题意: 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小冰的N个机器人兄弟排成一列,每一个机器人有一个颜色. 如今小冰想让同一颜色的机器人聚在一起.即随意两个同颜色的 ...