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

92
Visualizações
Passing props from one class component to other in react js

I am taking a beginner's course on react js. I came across class components and I wanted to know how does the props object get initialized.

for eg:

Main Component:

class App extends React.Component {
    render() {
        return (
            <div>
                <Header username="xyz"/>
            </div>
        )
    }
}

Header:

class Header extends React.Component {
    render() {
        return (
            <div>
                <p>Welcome, {this.props.username}</p>
            </div>
        )
    }
}

I wanted to know does the props object in Header class is getting initialized?

Shouldn't it be something like this:

class Header extends React.Component {
    constructor(props) {
        super()
        this.props = props
    }
    render() {
        return (
            <div>
                <p>Welcome, {this.props.username}</p>
            </div>
        )
    }
}

By using the above code I am a getting a message:

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

My question is, if we want to pass anything to a class, shouldn't it be done using a constructor?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

By extending React.Component, React does this for you.

The props object should never be changed inside the receiving component. React class components will gather all the arguments you pass to a component, and refer to them by the common name "props". This is why you don't have to do it yourself, in the constructor.

However, in modern React, class components are rarely used. The reason is that class components easily ends up with complicated lifecycle management.

The Header component, written as a function rather than a class, would look like this:

const Header = (props) => (
  <div>
      <p>Welcome, {props.username}</p>
  </div>
)

It is also very common to destructure props directly, so that you never really access the props object, like this:

const Header = ({username}) => (
  <div>
      <p>Welcome, {username}</p>
  </div>
)

In the case of function components, you're free to use whatever name you want for the props. I wouldn't do it though, because people are used to props being called props. But this would work as well:

const Header = (params) => (
  <div>
      <p>Welcome, {params.username}</p>
  </div>
)
about 4 years ago · Juan Pablo Isaza Relatório

0

I used the same code as you post without constructor and I don't have any error.

You can pass what you want to a class without using a constructor like you did in your example code.

I share the link of your code in sandbox. I put the constructor in Header to show that it returns the same with it and without it.

about 4 years ago · Juan Pablo Isaza 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