Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

render profile image in another component – react js

I’m trying to learn how to use props in react js with making a comment post, but I can’t seem to make the default profile image, from the ProfilePicture component render in the Comment component. Please help (°Ω°)/, thanks in advance.

const ProfilePicture = (props) => {
  return (
    <div>
      <img
        className="ProfilePicture"
        src={props.image.pfpUrl}
        alt={props.image.name}
        height={100}
        width={100}
        />
    </div>
  )
};

// user's comment
const comment = {
  image: {
    name: "Default Profile Image",
    pfpUrl: 'https://static.vecteezy.com/system/resources/thumbnails/009/292/244/small/default-avatar-icon-of-social-media-user-vector.jpg'
  }
}

const Comment = (props) => {
  return (
    <div>
      <ProfilePicture image={comment.pfpUrl}/>
    </div>
  )
}

ReactDOM.render(
  <Comment image={comment.image} />,
  document.getElementById('root')
);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re passing a prop called image here:

<Comment image={comment.image} />

But nowhere in the <Comment> component do you use that prop. Instead, you use comment directly:

<ProfilePicture image={comment.pfpUrl}/>

And comment has no property called pfpUrl. If you want to use the object directly, use the correct property:

<ProfilePicture image={comment.image.pfpUrl}/>

(And of course remove the prop since it’s unused.)

If you want to use the prop, use the prop:

<ProfilePicture image={props.image}/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading