I am making currency converter with react. I added swap button, which task is to change currency users want to sell with the one they want buy. So I have to change rates whenever its done here is function responsible for that:
getRates(currencyShortcut){
fetch('https://api.fixer.io/latest?base='+currencyShortcut)
.then(response => response.json())
.then(parsedJson => this.setState({currencyRates:parsedJson}))
.catch(error => console.log('fetch error - ', error))
}
and swap function looks like that:
swapValues(e){
e.preventDefault();
const {currency1,currency2} = this.state;
this.getRates(currency2.shortcut,()=>{
this.setState({
currency1: currency2,
currency2:currency1
})
});
}
but despite getRates is executed, the callback where I set state isn't. I seen When to use React setState callback, I want to do the same but another way around.