[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 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的更多相关文章
- [React] Use Prop Collections with Render Props
Sometimes you have common use cases that require common props to be applied to certain elements. You ...
- [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 ...
- [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 ...
- React Render Props 模式
概述 Render Props模式是一种非常灵活复用性非常高的模式,它可以把特定行为或功能封装成一个组件,提供给其他组件使用让其他组件拥有这样的能力,接下来我们一步一步来看React组件中如何实现这样 ...
- 可复用 React 的 HOC 以及的 Render Props
重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品(领导)打脸,真的最后一次改需求,我们烦恼于频繁修改的需求 虽然我们不能改变别人,但我们却可以尝试去做的更好,我们需要抽象,封装 ...
- React 之 render props 的理解
1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...
- React中render Props模式
React组件复用 React组件复用的方式有两种: 1.render Props模式 2.高阶组件HOC 上面说的这两种方式并不是新的APi. 而是利用Raect自身的编码特点,演化而来的固定编码写 ...
- 关于React.PropTypes的废除,以及新版本下的react的验证方式
React.PropTypes是React用来typechecking的一个属性.要在组件的props上运行typechecking,可以分配特殊的propTypes属性: class Greetin ...
- React高阶组件 和 Render Props
高阶组件 本质 本质是函数,将组件作为接收参数,返回一个新的组件.HOC本身不是React API,是一种基于React组合的特而形成的设计模式. 解决的问题(作用) 一句话概括:功能的复用,减少代码 ...
随机推荐
- STM32的PWM输入模式设置并用DMA接收数据
参考 :STM32输入捕获模式设置并用DMA接收数据 PWM input mode This mode is a particular case of input capture mode. The ...
- C# MD5 32位加密 UTF-8编码
项目开发过程中需要用到MD5加密,最开始的使用使用加密方法: public static string GetMD5(string str) { byte[] b ...
- mui选择器和dom获取元素的区别(记得把mui对象转为dom对象才能调用用dom方法)
<!DOCTYPE html><html> <head><meta charset="UTF-8"><meta name=&q ...
- 关于pcie的备忘
总线驱动:深度优先统计资源,深度滞后分配资源 资源包括Bus id和内存(prefectable和non-prefectable内存) 设备驱动:包括设备驱动层和消息通信 主要是四个部分: (1)中断 ...
- C#编程(十七)----------Object类
Object类 它是.NET Framework 中所有类的最终基类:它是类型层次结构的根.也就是说所有的类都拥有object类的方法,并能重写,调用. object的构造函数:public Obje ...
- Reflector_8.3.0.93_安装文件及破解工具
Reflector_8.3.0.93_安装文件及破解工具 下载地址:http://pan.baidu.com/s/1jGwsYYM 约 8.9MB
- SOA:A note on RPC
原文地址:http://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html. Although RPC is a pretty common pat ...
- Spring Framework 4.1.3 还是一样给力
Spring Framework 4.1.3 发布,此版本是 4.1.x 系列的第三个维护版本,包括超过50 个 bug 修复和改进.本来是计划月末发布,但是想早些发布,配合这周要发布的 Spring ...
- [C#技术] DataSet(DataTable)轻松的通过Sum、Aver、Count等统计出相关结果
我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...
- 使用Logstash创建ES映射模版并进行数据默认的动态映射规则
本文配置为 ELK 即(Elasticsearch.Logstash.Kibana)5.5.1. Elasticsearch 能够自动检测字段的类型并进行映射,例如引号内的字段映射为 String,不 ...