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

Passing component to another component, props doesn't show up – react js

I’m trying to pass the ‘Username’ component directly into the ‘Comment’ component, like I did with the ‘ProfilePicture’ component, but only the ‘@’ symbol shows up and not the username. Please help, also thanks in advance!

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

// username component
const Username = (props) => {
  return (
    <div>
      <p>@{props.user.username}</p>
    </div>
  )
};

// user's comment
const comment = {
  username: "guest",
  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 user={props.image} />
      <Username user={props.username} />
    </div>
  )
}

ReactDOM.render(
  <Comment image={comment.image} username={comment.username} />,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root">
    <!-- This element's contents will be replaced with your component. -->
</div>

>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 need to access it like props.user in your Username component

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

// username component
const Username = (props) => {
  return (
    <div>
      <p>@{props.user}</p> // YOU are passing user as a prop to username component
    </div>
  )
};

// user's comment
const comment = {
  username: "guest",
  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 user={props.image} />
      <Username user={props.username} />
    </div>
  )
}

ReactDOM.render(
  <Comment image={comment.image} username={comment.username} />,
  document.getElementById('root')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root">
    <!-- This element's contents will be replaced with your component. -->
</div>
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