-2

When I trying to update my state with setState it doesn't work.

sendMax = () => {
    console.log('Balance',this.state.balance)
    this.setState({ amount: this.state.balance })
    console.log('max', this.state.amount)
  }



<TouchableOpacity onPress={() => this.sendMax()}>
                <Image
                  source={require('../../assets/maxIcon.png')}
                  style={styles.icons} />
              </TouchableOpacity>

I'm using it inside of this how can I get this value inside it

when I console this.state.balance I get the value but I cannot set amount with this value

Atakan
  • 97
  • 10

1 Answers1

1

setState is async, if you want to listen when it's updated, use a callback

this.setState({ amount: this.state.balance }, () => console.log('max', this.state.amount));

Vladyslav Babenko
  • 1,349
  • 18
  • 26
  • sendMax = () => { console.log('Balance',this.state.balance) this.setState({ amount: this.state.balance },() => console.log('max', this.state.amount) ) } this.sendMax()}> – Rishav Kumar Sep 02 '19 at 05:39