map不是本机中的函数

问题描述 投票:0回答:2

我在映射数组时遇到问题

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:

请帮助。

javascript arrays react-native react-native-android picker
2个回答
0
投票

技术上.map()要求在变量上执行数组。

我想.map()不是要传递给itemValue的数组。这就是为什么您有该错误。我认为,如果您修改代码以将handleSelect处理为数组,则它应该可以工作。

currencies

我希望有帮助!


0
投票

[很有可能不是handleSelect = itemValue => { this.setState( { ...this.state, currencies: [itemValue], result: null, }, this.calculate, ); }; 不是数组。这是我推荐的:

  • [检查以确保您之前在渲染功能中正确地破坏了状态,例如currencies这里的任何错字都可能导致错误。
  • 确保const { currencies } = state;设置为默认设置。如果在首次安装组件时currencies未定义,则会出现此错误。
  • 使用currencies,或调试器查看console.log的值。如果在调用currencies方法时它不是数组(包括undefined),则将出现此错误。
© www.soinside.com 2019 - 2024. All rights reserved.