【问题标题】:Getting text from ModelDropdown in react native app development在反应原生应用程序开发中从 ModelDropdown 获取文本
【发布时间】:2018-09-17 05:50:23
【问题描述】:

我想知道我们如何在选择选项时从 ModelDropdown 中检索文本:

import ModalDropdown from 'react-native-modal-dropdown';
...

export default class App extends React.Component {

constructor(props) {
super(props);
this.state = {
  category: '',    
}
}

updateCategory(newCategory) {
this.setState({
  category: newCategory
})
}

....

<ModalDropdown 
        style={{padding: 20}}
        options={['Electronics', 'Furniture']}
        textStyle={{fontSize: 20, color: 'black', fontWeight: 'bold',}}
        dropdownTextStyle={{fontSize: 20, backgroundColor: '#FFF', color: 'black'}}
        defaultValue = 'Select Category'
        onSelect={(newCategory) => this.updateCategory(newCategory)}
/>

我正在获取索引值,例如“0”代表电子产品,“1”代表家具。 我想获取与该索引对应的文本。

或者是否有任何替代 ModelDropdown 的方法可以帮助我完成这项任务?

【问题讨论】:

    标签: javascript reactjs react-native mobile


    【解决方案1】:

    由于您获得了相应的索引,因此您可以轻松地从选项数组中获取值,只需将其设置为状态或一些全局变量。 你可以做这样的事情。

    constructor(props) {
        super(props);
        this.state = {
            options:['Electronics', 'Furniture'],    
        }
    }
    

    然后在 updateCategory 中取这样的值

    updateCategory(newCategory) {
        this.setState({
          textValue: this.state.options[newCategory]
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多