0

I have a function as following:

changeLocale = (e) => {
    console.log(e.target.value)
    this.setState({
        localIndex: e.target.value
    });

    console.log('localIndex', this.state.localIndex)
    this.setValue({id:"locale_id", value:this.props.metadata["latest"].locales[this.state.localIndex].id, error:null})
}

console.log(e.target.value) prints 0, however second print statement prints null. Why my state is not set for localIndex? What am I doing wrong?

Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
Thinker
  • 5,326
  • 13
  • 61
  • 137
  • what does this.state print if you log it out? – maxwell Jun 06 '17 at 20:26
  • ah yes, you will see the value if you put the console.log in the optional callback you can provide to this.setState({ change state here}, () => console.log(this.state.localIndex)) – maxwell Jun 06 '17 at 20:27
  • I guess I got the right way by adding a function(){ ** and here seting the value**} – Thinker Jun 06 '17 at 20:28
  • See if [the setState docs](https://facebook.github.io/react/docs/react-component.html#setstate) help you out. setState() doesn't immediately update state, use componentDidUpdate to check if update occurred. – Josh G Jun 06 '17 at 20:30

0 Answers0