Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
ReactJS - Component not passing this.state.object properly to another component

I have a User component:

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;

Then I am using this component in my navbar component:

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;

However instead of getting the username and the profile picture I am getting nothing (as if no data is being passed to the function).

What am I doing wrong

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In order to access passed props, 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 Relatório

0

or better - make it a basic stateless component:

const User = ({username, profilePicture}) => (
   <div>
       <span className="user-name">{username}</span>
       <img src={profilePicture} />
  </div>
)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda