Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
ReactJS: el componente no pasa this.state.object correctamente a otro componente

Tengo un componente de usuario:

 import React, { Component } from 'react'; class User extends Component { render() { return( <div> <span className="user-name">{this.username}</span> <img src={this.profilePicture} /> </div> ) } } User.propTypes = { username: React.PropTypes.string.isRequired, profilePicture: React.PropTypes.string.isRequired }; export default User;

Luego estoy usando este componente en mi componente de barra de navegación:

 import React, { Component } from 'react'; import User from './User.component'; import mockProfilePicture from './mockProfilePicture.jpg'; class Navbar extends Component { constructor(props) { super(props); this.state = { user: { username: 'John', profilePicture: mockProfilePicture } } } renderUser = (user) => { return ( <User username={user.username} profilePicture={user.profilePicture}/> ) }; render() { return( <div> {this.renderUser(this.state.user)} </div> ) } } export default Navbar;

Sin embargo, en lugar de obtener el nombre de usuario y la imagen de perfil, no obtengo nada (como si no se pasaran datos a la función).

Qué estoy haciendo mal

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Para acceder a los accesorios pasados, use need to use this.props :

 class User extends Component { render() { return( <div> <span className="user-name">{this.props.username}</span> <img src={this.props.profilePicture} /> </div> ) } }
over 4 years ago · Santiago Trujillo Report

0

o mejor, conviértalo en un componente sin estado básico:

 const User = ({username, profilePicture}) => ( <div> <span className="user-name">{username}</span> <img src={profilePicture} /> </div> )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!