简体   繁体   English

如何将接口/类型动态传递给打字稿中的常见反应函数

[英]How to pass interface/type dynamically to a common react function in typescript

I have created a common function to render a table.我创建了一个通用函数来呈现表格。

Table.tsx表.tsx


interface Type {
  name: string;
  age: number;
}

interface PropType {
  column: Array<Type>;
}

function Table({ column }: PropType) {
  return null;
}

export default Table;

Now I am using this component for two different pages, User and Product.现在我将这个组件用于两个不同的页面,用户和产品。 both user and product page has different data to be rendered on table component用户和产品页面都有不同的数据要在表组件上呈现

User.tsx用户.tsx

import Table from "./Table";

interface InterfaceUser {
  name: string;
  age: number;
}

const users: Array<InterfaceUser> = [
  {
    name: "John",
    age: 21
  }
];
function User() {
  return <Table column={users} />;
}
export default User;

User.tsx works well as we have created the same interface in table component but when it comes to Product.tsx page, we get errors. User.tsx 运行良好,因为我们在表组件中创建了相同的界面,但是当涉及到 Product.tsx 页面时,我们会遇到错误。

import Table from "./Table";

interface InterfaceProduct {
  title: string;
  age: number;
}

const products: Array<InterfaceProduct> = [
  {
    title: "Product name",
    price: 21
  }
];
function Product() {
  return <Table column={products} />;
}
export default Product;

I am new to typescript, any help would really be appreciated.我是打字稿的新手,非常感谢任何帮助。

You can use Typescript generic type like this:您可以像这样使用 Typescript 通用类型:

In Table.tsx :Table.tsx

interface PropType<ColumnsType> {
  column: ColumnsType[];
}

function Table<ColumnType>({ column }: PropType<ColumnType>) {
  return null;
}

in User.tsx :User.tsx中:

function User() {
  return <Table<InterfaceUser> column={users} />;
}

in Product.tsx :Product.tsx中:

function Product() {
  return <Table<InterfaceProduct> column={products} />;
}

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

相关问题 React + Typescript:如何为函数动态设置通用接口? - React + Typescript: How can I dynamically set a generic interface for a function? 如何在TypeScript中动态扩展对象的接口或类型? - How to dynamically extend the interface or type of an object in TypeScript? 在 React/Typescript 中将元素作为类型传递给 function - Pass Element as a type in function in React/Typescript 如何传递带有参数的 function 与 typescript 反应 - How to pass a function with parameters in react with typescript function 的 Typescript 参数作为接口或类型 - Typescript argument of function as interface or type 如何与一个普通的HTML网站共享一个反应项目的打字稿功能? - How to share a typescript function from a react project with a common html website? 如何在 React 中根据 typescript 接口检查 JSON object 的类型 - How to check type of a JSON object against typescript interface in React Typescript:如何在不重新定义类型的情况下将复杂类型的变量传递给 function - Typescript: how to pass variable with complex type to a function without redefining type Typescript - 将接口文字传递给函数参数 - Typescript - Pass interface literals to function parameters 在 Typescript/React 中使用一种或另一种接口类型 - Use one or another interface type in Typescript/React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM