简体   繁体   English

typescript中组件的React实例的正确类型是什么

[英]what is the correct type for a React instance of a component in typescript

Say I have a component that takes 2 button instances: 假设我有一个带有2个按钮实例的组件:

export interface GridProps {
  backButton: any;
  nextButton: any;
}

Should the type for the button be React.ReactNode or React.ReactElement 按钮的类型应该是React.ReactNode还是React.ReactElement

so either: 所以要么:

export interface GridProps {
  backButton: React.ReactNode;
  nextButton: React.ReactNode;
}

or 要么

export interface GridProps {
  backButton: React.ReactElement<any>;
  nextButton: React.ReactElement<any>;
}

React.ReactNode is more convenient way for passing markup to child components. React.ReactNode是将标记传递给子组件的更方便的方法。 It contains everything you can insert in JSX like <div>{passedContent}</div> It is used in many React component libraries. 它包含您可以在JSX中插入的所有内容,如<div>{passedContent}</div>它在许多React组件库中使用。 It makes sense to use React.ReactElement<any> only if you want to restrict customization to elements of particular class. 仅当您要将自定义限制为特定类的元素时,才有意义使用React.ReactElement<any> For example React.ReactElement<ButtonProps> . 例如React.ReactElement<ButtonProps> It is useful if you want to clone passed element and append some props to it. 如果你想克隆传递的元素并为它添加一些道具,它会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM