简体   繁体   English

使用键值对在React Native中填充Picker时,“未定义不是'this2.props.options [key]'上的对象”

[英]“Undefined is not an object on 'this2.props.options[key]'” when using key-value pair to populate Picker in react Native

I am trying to populate a picker with a set of data from a key-value pair. 我试图用来自键值对的一组数据填充选择器。


    const options = {
      inch: 0.0254,
      foot: 0.3048,
      yard: 0.9144,
      mile: 1609.34
    };
return (
      <View>
        <Picker
          mode="dropdown"
          selectedValue={this.state.fromUnit}
          onValueChange={(unit, value) => this.setState({ fromUnit: value })}
        >
          {Object.keys(options).map(key => {
            return (
              <Picker.Item
                label={this.props.options[key]}
                value={key}
                key={key}
              />
            );
          })}
        </Picker>

Upon running my code, I get the following: 运行代码后,我得到以下信息:

TypeError: undefined is not an object (evaluating '_this2.props.options[key]')

I have tried adding options[] to state, but that didn't seem to make any difference. 我曾尝试添加options []来声明状态,但这似乎没有任何区别。

Not sure what is going on here, it looks pretty straight forward to me. 不知道这里发生了什么,对我来说看起来很直白。 It seems that it's getting caught up on the 'key' part of the mapping but I am unsure why. 看来它已经陷入了映射的“关键”部分,但我不确定为什么。 any advice would be appreciated 任何意见,将不胜感激

You can try this 你可以试试这个

    const options = [
      {name: inch,  value: 0.0254},
      {name: foot, value: 0.3048},
      {name: yard, value: 0.9144},
      {name: mile, value: 1609.34}
    ];

         {options.map((options, key) => {
            return (
              <Picker.Item
                label={options.name}
                value={options.value}
                key={key}
              />
            );
          })}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM