简体   繁体   中英

Using Interfaces as an attribute in TypeScript React

I have a class called Tab which has three props:

  • num: string
  • desc: string
  • parts: Part[]

where Part has this code in Tab.tsx :

interface Part {
    desc: string,
    link: string
}

But when I create a Tab in Menu.tsx (the parent), I am unsure how to define the parts attribute.

<Tab num="1" desc="Description" parts=? />

How do I go about this?

parts is an Array of objects in the shape of { desc: string; link: string; } { desc: string; link: string; } { desc: string; link: string; } .

Pass it this way: [{ desc: "foo", link: "bar" }, { desc: "foo", link: "baz" }] .

Example:

<Tab
 num="1"
 desc="Description"
 parts={[{ desc: "foo", link: "bar" }, { desc: "foo", link: "baz" }]}
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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