0

I have my Homepage which sends a modal component a profile like this.

<ProfileOverviewModal
     open={openProfile}
     onClose={closeAllModals}
     onCreateProfile={onCreateProfile}
     profile={state.profil}
/>

In my ProfileOverviewModal.tsx I have this props

type Props = {
  open: boolean
  onClose: () => void
  onCreateProfile: (values: ProfileModalFormValues) => void
  profile: Profil[]
}

When I console.log(profile) everything seems to work. This shows up in the console:

{Firstname: 'Eddy', Lastname: 'Browkin', __typename: 'Profil'}

But I can not access profile.Firstname && profile.Lastname for example

return (<h1>{profile.Firstname}</h1>)

Why is that? Any one can point me in the right direction.

3
  • Why are you expecting it to be an array profile: Profil[] and excessing like single object? Commented May 29, 2022 at 8:17
  • I am fairly new. How do I access this type of types? Commented May 29, 2022 at 8:21
  • If your types are correct you're trying to read FirstName on an array (which is undefined). You'll need to map over profile, e.g. return profile.map((prof) => <h1>{prof.firstName}</h1>) Commented May 29, 2022 at 8:49

3 Answers 3

1

Your profile prop expects array of object of type Profil, if you are passing single object then just change the prop as

type Props = {
  open: boolean
  onClose: () => void
  onCreateProfile: (values: ProfileModalFormValues) => void
  profile: Profil
}

And if you are passing array of profile then change you way you are accessing as below

return (<h1>{profile[index].Firstname}</h1>)
Sign up to request clarification or add additional context in comments.

Comments

0

The issue is that you have defined the type as Profile[] which is expecting array of Profiles. Just remove the [] from the type and you won't be seeing any error.

Comments

0

Try this

return (
   <div>
      <h1> {profile.Firstname} </h1>
   </div>
)

Either use div tag or fragment i.e <> here h1 tag or everything else </>. Also. in props , onCreateProfile does'nt seem to be correct. Please check that also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.