简体   繁体   English

如何定义React方法的特定返回类型(打字稿)

[英]How to define specific return type of react method (typescript)

I want to define a return type of a specific react component. 我想定义特定反应组件的返回类型。

For Example: 例如:

Component 1 组件1

class ComponentFoo extends React.Component<any, any> {
   public render() {…}
}

Component 2 组成部分2

class ComponentBar extends React.Component<any, any> {
    public renderComponentFoo() : ComponentFoo { <---
      return <ComponentFoo />
    }

    public render() {
      …
      {this.renderComponentFoo()}
      …
    }
}

Is something like this possible? 这样的事情可能吗?

Your renderComponentFoo method should return React.ReactNode instead. 您的renderComponentFoo方法应该返回React.ReactNode

JSX/TSX is just syntax sugar around React.createElement() which returns React.ReactNode . JSX / TSX只是React.createElement()语法糖,它返回React.ReactNode You are not returning an instance of ComponentFoo as your current code suggests. 您没有按照当前代码的建议返回ComponentFoo的实例。

暂无
暂无

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

相关问题 如何在 TypeScript 定义中定义 React FunctionalComponent 的返回类型 - How to define the return type of React FunctionalComponent in TypeScript definition 如何在 react/typescript 中定义按钮的类型? - How to define the type of a button in react / typescript? 如何定义返回相同类型结构的多个 TypeScript 接口? - How to define multiple TypeScript Interfaces that return the same type structure? 如何定义类型<video>使用 Typescript 在 React 中引用?</video> - How can I define the type for a <video> reference in React using Typescript? 在 React Typescript 中,如何为原生 html 元素定义 ...rest 参数的类型? - In React Typescript how to define type for …rest parameter for native html elements? 如何在 React TypeScript 中定义一次类型并将其分配给函数和组件? - How to define a type once and assign it to a function and a component in React TypeScript? React孩子是一个函数:如何正确定义TypeScript类型? - React children is a function: How to define TypeScript type correctly? 如果接口类型特定,我们如何在react typescript中初始化状态 - How can we initialise a state in react typescript in constructor method, If it has a specific interface type TypeScript:在 React 中的 props 中传递组件时如何定义类型? - TypeScript: How to define the type when pass component in props in React? React - 如何使用打字稿定义道具 - React - How to define props with typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM