-1

Here is my code. But in render function, the age is coming correctly

 const newDate = moment().toISOString();
    const tempAge = moment(newDate).diff(moment(nextProps.doctor.specificPatient.Dob).toISOString(), 'years');
    console.log('const',tempAge);
    this.setState({age: tempAge});
    console.log('moment',this.state.age);
Pravin
  • 195
  • 1
  • 6
  • In first log the age is coming. but when i tried to display it after assigning it to state, it is showing blank. – Pravin Jun 14 '18 at 08:10
  • @Pravin write it like this using callback, you will see the updated value: `this.setState({age: tempAge}, () => console.log('updated state value', this.state.age));` – Mayank Shukla Jun 14 '18 at 08:18
  • Its not duplicate. The behaves differently with debugger – Pravin Jun 14 '18 at 10:04

1 Answers1

0

I seriously suggest you read the docs of each method you use from a third party library. You can't go on just expecting behaviors. You need to be sure of the outcome, when it is executed and so on.

From the docs:

Think of setState() as a request rather than an immediate command to update the component. For better perceived performance, React may delay it, and then update several components in a single pass. React does not guarantee that the state changes are applied immediately.

Adelin
  • 7,809
  • 5
  • 37
  • 65
  • When i display it without debugger is on it shows blank, But when i start the debugger it is showing correctly in screen. In either case the log is coming blank – Pravin Jun 14 '18 at 08:18