I am making a currency converter app. My issue is in my function Currency Converter. The AUD that I have as a string in the useState is not being read after i launch the app. I am not sure why as the code was working this morning. But I added a variable and function to convert after someone inputs an amount and that’s when I received the error.
`import React, { useEffect, useState } from 'react';
import Dropdown from './Dropdown';
const Base_URL = 'https://api.frankfurter.app/latest?from='
function CurrencyConverter() {
const [currencyOptions, setCurrencyOptions] = useState([])
const [fromCurrency, setFromCurrency] = useState("USD")
const [toCurrency, setToCurrency] = useState("AUD")
const [exchangeRate, setExchangeRate] = useState()
const [amount, setAmount] = useState(1)
const[amountInFromCurrency, setAmountInFromCurrency] = useState(true)
let toAmount, fromAmount
if(amountInFromCurrency){
fromAmount = amount
toAmount = amount*exchangeRate
} else{
toAmount = amount
fromAmount = amount/exchangeRate
}
//console.log(exchangeRate)
useEffect(() => {
fetch(Base_URL+fromCurrency)
.then(res => res.json())
.then(data => {
setCurrencyOptions([data.base, ...Object.keys(data.rates)])
setExchangeRate(data.rates)
})
}, [fromCurrency])
return (
<div className="container">
<div className="text-center p-3 mb-2">
<h2 className="mb-2">Currency Converter</h2>
</div>
<div className="row text-center">
<Dropdown
currencyOptions = {currencyOptions}
selectedCurrency={fromCurrency}
onChangeCurrency={e => setFromCurrency(e.target.value)} //<- supposed to take the target event(currency selection) and change it to that option
/>
<div className="equals">{amount}={amount*exchangeRate[toCurrency]}</div>
<Dropdown
currencyOptions = {currencyOptions}
selectedCurrency={toCurrency}
onChangeCurrency={e => setToCurrency(e.target.value)} //<- supposed to take the target event(currency selection) and change it to that option
/>
</div>
</div>
)
}
export default CurrencyConverter;`
To solve it I deleted the code I added in and just reverted everything back to the way I had it before but the error is still there.
Here is the code that I think changed everything:
let toAmount, fromAmount
if(amountInFromCurrency){
fromAmount = amount
toAmount = amount*exchangeRate
} else{
toAmount = amount
fromAmount = amount/exchangeRate
}
As an extra note this code is from my App.js, please let me know if it would be helpful for me to include my Dropdown.js as well