我在映射数组时遇到问题
class CurrencyCashScreen extends React.Component {
state = {
currencies: ['USD', 'AUD', 'SGD', 'PHP', 'EUR'],
base: 'PLN',
amount: '',
convertTo: 'EUR',
result: '',
date: '',
};
所以您看到有一个带字符串的curriences数组。我正在使用Picker在它们之间进行选择,而我具有的功能是Picker中onValueChange属性所包含的,所以:
handleSelect = itemValue => {
this.setState(
{
...this.state,
currencies: itemValue,
result: null,
},
this.calculate,
);
};
然后是我的地图问题
<Picker
selectedValue={convertTo}
value={convertTo}
onValueChange={(itemValue, itemIndex) =>
this.handleSelect(itemValue)
}>
{currencies.map((currency) => (
<Picker.Item label={currency} value={currency}>
{currency}
</Picker.Item>
))}
</Picker>
also there is a error message:
请帮助。
技术上.map()
要求在变量上执行数组。
我想.map()
不是要传递给itemValue
的数组。这就是为什么您有该错误。我认为,如果您修改代码以将handleSelect
处理为数组,则它应该可以工作。
currencies
我希望有帮助!
[很有可能不是handleSelect = itemValue => {
this.setState(
{
...this.state,
currencies: [itemValue],
result: null,
},
this.calculate,
);
};
不是数组。这是我推荐的:
currencies
这里的任何错字都可能导致错误。const { currencies } = state;
设置为默认设置。如果在首次安装组件时currencies
未定义,则会出现此错误。currencies
,或调试器查看console.log
的值。如果在调用currencies
方法时它不是数组(包括undefined
),则将出现此错误。