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

132
Visualizações
Unable to setState with props in class component

I have a class component which can be viewed by 2 screens and 1 screen is passing props and 1 is not. so i want to set default value "" 0r empty if there are no props and if there are props then value should be based on props. but i'm unable to do so. Any help will be appreciated.. flow is like this.. main screen==> rating(without props so state should be empty)==>search screen==>rating(with props. state should get value from state)

class Rating extends Component {
constructor() {
super();
const propItem = this.props?.route.params.item;
this.state = {
brand : propItem != undefined ? propItem.brand : "",

i have tried componentDidMount but it cause infinite loop so does if i uset it in render method

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

0

Try getDerivedStateFromProps

getDerivedStateFromProps is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to pass the pros for constructor and super function.

class Rating extends Component {
  constructor(props) {
    super(props);
    const propItem = this.props?.route.params.item;

    this.state = {
      brand : propItem != undefined ? propItem.brand : ""
    }
  }

  render() {
    <div></div>
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

class Rating extends Component {
constructor(props) {
    super(props);

this.state = {
  brand : this.props?.route.params.item != undefined ? this.props?.route.params.item.brand : ""
}}
render() {
<div></div>
}}

you can try like it, make sure your are passing item from previous screen in this.props.navigation

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