map 不是 react native 中的函数和选择器问题

map is not a function and picker problem in react native

我在映射我的关于货币的数组时遇到问题。 所以你看到有一个带有字符串的 curriences 数组。我使用 Picker 在它们之间进行选择,并且我具有 Picker 中 onValueChange 道具所包含的功能,然后我的问题是从选择器中选择一个项目。

首先我可以从选择器中选择任何项目,但是当我想再次选择时,我在列表中只有这个选择的项目:

然后我选择了欧元。当我想再次选择一个项目时,我只有这个:

此外,当我更改第一个选择器项目时 - 它也会在第二个选择器中发生变化...不知道为什么。

同时在此处添加完整代码:

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,
          });
        });
    }
  };

  handleSwap = e => {
    const base = this.state.base;
    const convertTo = this.state.convertTo;
    e.preventDefault();
    this.setState(
      {
        ...this.state,
        convertTo: base,
        base: convertTo,
        result: null,
      },
      this.calculate,
    );
  };
  render() {
    const {currencies, base, amount, convertTo, result} = this.state;
    return (
      <View>
        <Text>
          {amount} {base} is equevalent to
        </Text>
        <Text>
          {amount === '' ? '0' : result === null ? 'Calculating...' : result}{' '}
          {convertTo}
        </Text>
        <View>
          <View>
            <View>
              <TextInput
                keyboardType="numeric"
                value={amount}
                onChangeText={this.handleInput}
              />
              <Picker
                selectedValue={base}
                value={base}
                onValueChange={this.handleSelect}>
                {currencies.map((currency, index) => (
                  <Picker.Item label={currency} value={currency}>
                    {currency}
                  </Picker.Item>
                ))}
              </Picker>
            </View>
            <View>
              <TextInput
                editable={false}
                value={
                  amount === ''
                    ? '0'
                    : result === null
                    ? 'Calculating...'
                    : result
                }
              />
              <Picker
                selectedValue={convertTo}
                value={convertTo}
                onValueChange={this.handleSelect}>
                {currencies.map(currency => (
                  <Picker.Item label={currency} value={currency}>
                    {currency}
                  </Picker.Item>
                ))}
              </Picker>
            </View>
          </View>
          <View>
            <Text onClick={this.handleSwap}>CLICK ME</Text>
          </View>
        </View>
      </View>
    );
  }
}

export default CurrencyCashScreen;

请帮忙。

技术上 .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 的值。如果你调用currencies.map方法的时候不是数组(包括undefined),那么就会报这个错误。

见下例

你需要两个独立的函数

import React, {Component} from 'react';
import {View, Text, TextInput, Picker} from 'react-native';

class CurrencyCashScreen extends Component {
 state = {
    currencies: ['USD', 'AUD', 'SGD', 'PHP', 'EUR'],
    base: 'PLN',
    convertTo: 'EUR',
    amount: '',
    result: '',
    date: '',
  };

 handleSelectPicker1 = itemValue => {
    this.setState(
      {
      base:itemValue
      },
    );
  };

  handleSelectPicker2 = itemValue => {
    this.setState(
      {
        convertTo:itemValue
      },
    );
  };

render() {
    const {currencies, base, amount, convertTo, result,pickerValue1,pickerValue2} =     this.state;
    console.log('this.state',pickerValue1,pickerValue2)
    return (
      <View style={{flex:1}}>
              <Picker
                selectedValue={base}
                //value={base}
                onValueChange={this.handleSelectPicker1}>
                {currencies.map((currency, index) => (
                  <Picker.Item label={currency} value={currency}/>
                ))}
              </Picker>

              <Picker
                selectedValue={convertTo}
                //value={convertTo}
                onValueChange={
                this.handleSelectPicker2
                }>
                {currencies.map(currency => (
                  <Picker.Item label={currency} value={currency}/>
                ))}
              </Picker>
          </View>
    );
  }

}

export default CurrencyCashScreen;

希望这对您有所帮助。根据您的要求更改此设置。有疑问欢迎留言。