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. [Winform]安装在C盘,无操作权限的一个解决办法

    摘要 在对winform打包,进行安装的时候,一般会采用默认的安装路径,默认安装在C:\Program Files\xx或者C:\Program Files(x86)目录下,但windows有一种安全 ...

  2. 基于设备树的TQ2440的中断(1)

    作者 姓名:彭东林 E-mail:pengdonglin137@163.com QQ:405728433 平台 板子:TQ2440 内核:Linux-4.9 u-boot: 2015.04 工具链: ...

  3. TXMLDocument use case (Delphi)

    Description This example illustrates the basic operations on an XML document. Code procedure CreateD ...

  4. UICollectionView 相关

    当数据不多,collectionView.contentSize小于collectionView.frame.size的时候,UICollectionView是不会滚动的 self.Cov.alway ...

  5. GO -- 正则表达式

    str := "880218end" match, _ := regexp.MatchString("\\d{16}", str) //六位连续的数字 fmt. ...

  6. Cocos2d-x V2.x版本对64bit的支持

    2015年2月1日后新提交的应用必须要支持64位架构. 我所使用的是cocos2d-x V2.0版本,而且源码有部分代码是修改过的.好在cocos2d-x官方已经放出了一个支持64位的2.2.6版本, ...

  7. spring mvc 接受前台json @RequestBody json 属性 空 使用 JsonProperty spring mvc 获取json转乘bean

    请给json序列序列化成的javabean 属性加上   @JsonProperty(value = "real_name")   否则 springmvc 可能接受不到数据 ja ...

  8. C++二维数组 取地址 复制给 二维指针

    本来应该是个简单的问题,但是我就不明白了,为什么会段错误了... #include<stdio.h> #define UINT32 unsigned int UINT32 NType1_X ...

  9. MAT(Memory Analyzer Tool)内存分析工具的使用

    开发.应用中老是会遇到OutOfMemory异常,而且常常是过一段时间内存才被吃光,这里可以利用java heap dump出jvm内存镜像,然后再对其进行分析来查找问题. 平常利用jmap -dum ...

  10. C#中的自动赋值

    工作中用到对同一个类型的对象的赋值,需要逐个属性的赋值赋过去,在网上找了很久没发觉合适的,就自己动手写了个,以做备忘用. protected void AutoAssign(object from , ...