Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda