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

126
Views
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 answers
Answer question

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 Report

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 Report

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 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!