Encountering an issue with the Picker picker component. There is an array of currencies as strings. Using the Picker to select items from this array, and have a function included in the onValueChange
prop in Picker. The problem arises when trying to select an item more than once.
Initially able to select any item from the picker. However, upon attempting to choose another item, only the previously selected item is displayed in the list: multiple items For example, if EUR is chosen first and then another selection is attempted, only this appears: enter image description here
Furthermore, changing the item in the first picker also alters the selection in the second picker...for unknown reasons.
The complete code is provided below:
import React, {Component} from 'react';
import {View, Text, TextInput, Picker} from 'react-native';
class CurrencyCashScreen extends React.Component {
state = {
currencies: ['USD', 'AUD', 'SGD', 'PHP', 'EUR'],
base: 'PLN',
amount: '',
convertTo: 'EUR',
result: '',
date: '',
};
handleSelect = (itemValue, itemIndex) => {
this.setState(
{
...this.state,
currencies: [itemValue],
result: null,
},
this.calculate,
);
};
handleInput = text => {
this.setState(
{
...this.state,
amount: text,
result: null,
date: null,
},
this.calculate,
);
};
calculate = () => {
const amount = this.state.amount;
if (amount === isNaN) {
return;
} else {
fetch(`https://api.exchangeratesapi.io/latest?base=${this.state.base}`)
.then(res => res.json())
.then(data => {
const date = data.date;
const result = (data.rates[this.state.convertTo] * amount).toFixed(4);
this.setState({
...this.state,
result,
date,
});
});
}
};
// Remaining code sections removed for brevity
}
export default CurrencyCashScreen;
Your assistance in resolving this issue would be greatly appreciated.