在反應原生中,我有一個表單(簡單字段 - 名稱,地址),並希望添加一個下拉列表以選擇用戶類型。我想從Firebase動態加載下拉列表。可能嗎 ?感謝任何幫助。React Native - 動態加載列表中的下拉列表
更新時間:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Picker,
} from 'react-native';
export default class Testpicker extends Component {
constructor(props) {
super(props);
types = [{userType: 'admin', userName: 'Admin User'}, {userType: 'employee', userName: 'Employee User'}, {userType: 'dev', userName: 'Developer User'}];
this.setState({userTypes: types});
}
loadUserTypes() {
return this.state.userTypes.map(user => (
<Picker.Item label={user.userName} value={user.userType} />
))
}
render() {
return (
<View>
<Picker
selectedValue={this.state.selectedUserType}
onValueChange={(itemValue, itemIndex) =>
this.setState({selectedUserType: itemValue})}>
// Dynamically loads Picker.Values from this.state.userTypes.
{this.loadUserTypes()}
</Picker>
</View>
)
}
}
AppRegistry.registerComponent('Testpicker',() => Testpicker);
現在,我收到錯誤消息 「null不是(評估 'this.state.selectedUserType')的對象。」 我在做什麼錯?
編輯我的答案! –