簡體   English   中英

如何將道具傳遞給組件

[英]How to pass props to a component

輸入'{數據:{標題:字符串; }[]; }' 不可分配給類型 'IntrinsicAttributes & PropsT'。 類型“IntrinsicAttributes & PropsT”上不存在屬性“數據”。

const ParentComp = () => {
  const values = [
    { title: 'someText' }
  ]
  return <ChildComp data={values} /> // WARNING
}

type PropsT = [
  { title: string }
]
const ChildComp = (data: PropsT) => {
  return <>{data[0].title}</>
}

有兩個問題:

const ChildComp = (data: PropsT) => {

在這里, data是給定的道具。 這是一個 object。 你可能打算解構這個:

const ChildComp = ({ data }: PropsT) => {

道具類型PropsT應該反映這一點:

type PropsT = {
    data: { title: string; }[];
};

另一個變化是[{ title: string }]{ title: string }[]

前者是一個只有一個元素的元組,而后者是一個數組。 元組具有固定長度,而 arrays 可以擁有任意數量的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM