簡體   English   中英

打字稿/反應傳遞道具錯誤“不存在於類型......”

[英]Typescript/React passing props error 'Does not exist on type…'

我在嘗試讓 Typescript 使用傳遞道具時遇到了一些困難。 這是我的代碼:

應用程序.tsx

export const ProductPage = (): JSX.Element => {
  const { id } = useParams<{ id: string }>();

  return (
    <UpdateProductPage id={id} /> //throws an error here for id
  )
}

更新產品頁面.tsx

interface PropType {
  id: string;
}

export const UpdateProductPage = ({ id }: PropType): JSX.Element => {
  console.log(id)
  //unrelated logic
}

所以基本上在控制台中,我收到一條錯誤消息:

Type '{ accessToken: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'accessToken' does not exist on type 'IntrinsicAttributes'.ts(2322)

我也嘗試通過執行UpdateProductPage: React.FC<PropType>聲明道具類型,但它仍然不起作用。

試試這個方法:

更新產品頁面.tsx

import { FC } from 'react';

interface PropType {
  id: string;
}

export const UpdateProductPage: FC<PropType> = ({ id }): JSX.Element => {
  console.log(id)
}

或更改接口以使用類型。

type PropType = {
  id: string;
}

補充: https : //fettblog.eu/typescript-react-why-i-dont-use-react-fc/

暫無
暫無

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

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