[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;
};

原文地址:https://www.cnblogs.com/Answer1215/p/10610597.html

时间: 2024-07-28 15:03:34

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

[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 of a React element. We'll also show that you can add additional properties not declared on the element.

【React源码学习】Suspense &amp;&amp; Children

Suspense 16.6 提供的一个feature,在线源码地址:https://github.com/facebook/react/blob/master/packages/react/src/React.js 在一个Suspense组件,它下面渲染了一个或者多个异步的组件,有任何一个组件throw了一个promise之后,在这个promise的resolved之前,都会显示fallback中的内容. 也就是说,在一个Suspense组件中,有多个组件,它要等它里面所有组件都resolved

深入浅出 React 的 HOC 以及的 Render Props

重复是不可能的,这辈子都不可能写重复的代码 当然,这句话分分钟都要被产品打脸,天天喊着改需求,老哥,这里改下可好? 所以,我们需要抽象,封装重复的功能或者逻辑,这样改需求,也不用到处改 那么这次我们来讲讲 React 里的高级组件 React 高级组件有两种方式: 使用高阶组件( Higher Order Component ==> HOC ) 子组件作为函数的模式( Render Props ) 高阶组件 首先来说说高阶组件,它不是 React 的提供的 API,它是模式,一种增强组件的模式,

React 之 render props 的理解

1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任意 状态提升.Context.Redux 等 而 render props 本质实际上是使用到了回调的方式来通信.只不过在传统的 js 回调是在构造函数中进行初始化(使用回调函数作为参数),而在 react 中,现在可以通过 props 传入该回调函数,就是我们所介绍的 render prop. 从结果论来说

React hooks能替代Redux,HOC和render props么?

最近开始学习React,记录一下心得. React hooks是16.8.0推出的,其目的是为了替换class,HOC,render props.同时网传hooks也将终结redux,那么本文将讨论hooks究竟能不能替换掉redux,HOC,render props. 1. Hooks替代Redux. 与其说Hooks替代Redux,不如说是Context在替代Redux.Context是16.3.0推出的(之前也有,但是API不一样且基本没人用).其实从Context开始,可以说不用redu

react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)

react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redux-saga的核心配置会加以讲解,通过这个项目,可以系统的了解react技术栈的主要知识,避免搭建一次后面就忘记的情况. 从webpack开始 思考一下webpack到底做了什么事情?其实简单来说,就是从入口文件开始,不断寻找依赖,同时为了解析各种不同的文件加载相应的loader,最后生成我们希望的

[Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose

This lesson takes the concept of render props and migrates it over to streaming props by keeping the same example and simple refactoring the Togglecomponent which handles the render prop. const ToggleStream = componentFromStream(props$ => { const { h

使用react context实现一个支持组件组合和嵌套的React Tab组件

纵观react的tab组件中,即使是github上star数多的tab组件,实现原理都非常冗余. 例如Github上star数超四百星的react-tab,其在render的时候都会动态计算哪个tab是被选中的,哪个该被隐藏: getChildren() { let index = 0; let count = 0; const children = this.props.children; const state = this.state; const tabIds = this.tabIds

[React Native] Disable and Ignore Yellow Box Warnings in React Native

Yellow box warnings in react native can be intrusive. We will use console.disableYellowBox to disable the yellow box entirely. We'll also use console.ignoredYellowBox to selectively disabled warnings. Disable all the yellow box warning: console.disab