Seems like a simple solution, and there is quite a lot of people that have the same question but the solutions don't make any sense to me. Whenever I click the button {rate} consolelogs blank the first time and then gives me a proper number the second time.
Here is the code:
import { useState } from 'react';
import './App.css';
function App() {
const [from, setFrom] = useState('')
const [to, setTo] = useState('')
const [rate, setRate] = useState('')
const calculate = () => {
fetch(`https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/${from}.json`)
.then(response => response.json())
.then(data => {
setRate(data[from][to])
})
console.log(rate)
}
return (
<div className="App">
<div className="container">
<input type="text" className="from" onChange={(e) => (setFrom(e.target.value))} />
<input type="text" className="to" onChange={(e) => (setTo(e.target.value))} />
<button onClick={() => calculate()}>calculate</button>
</div>
</div>
);
}
export default App;
Lmk what you guys think about it! anything will be appreciated. Thank you!