我有我的主頁,它向模態組件發送這樣的組態檔。
<ProfileOverviewModal
open={openProfile}
onClose={closeAllModals}
onCreateProfile={onCreateProfile}
profile={state.profil}
/>
在我的 ProfileOverviewModal.tsx 我有這個道具
type Props = {
open: boolean
onClose: () => void
onCreateProfile: (values: ProfileModalFormValues) => void
profile: Profil[]
}
當我 console.log(profile) 一切似乎都正常。這顯示在控制臺中:
{Firstname: 'Eddy', Lastname: 'Browkin', __typename: 'Profil'}
但我無法訪問 profile.Firstname && profile.Lastname 例如
return (<h1>{profile.Firstname}</h1>)
這是為什么?任何人都可以為我指明正確的方向。
uj5u.com熱心網友回復:
您的組態檔道具需要 Profil 型別的物件陣列,如果您傳遞單個物件,則只需將道具更改為
type Props = {
open: boolean
onClose: () => void
onCreateProfile: (values: ProfileModalFormValues) => void
profile: Profil
}
如果您要傳遞組態檔陣列,請更改您的訪問方式,如下所示
return (<h1>{profile[index].Firstname}</h1>)
uj5u.com熱心網友回復:
問題是您已經定義了Profile[]期望組態檔陣列的型別。只需[]從型別中洗掉,您就不會看到任何錯誤。
uj5u.com熱心網友回復:
嘗試這個
return (
<div>
<h1> {profile.Firstname} </h1>
</div>
)
使用 div 標簽或片段,即 <> 這里是 h1 標簽或其他所有標簽 </>。還。在 props 中,onCreateProfile 似乎不正確。請也檢查一下。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482154.html
上一篇:如何將自定義斷點添加到sx道具?
