首页 > 解决方案 > React Native RNPicker 选择

问题描述

我正在使用 RNPicker Select,我希望从 api 获取下拉项目,但我得到空的下拉列表,没有值以下是我的代码

class Component extends Component {
  constructor(props) {
    super(props);
    this.state = {
  inqSourceList: [],
}

componentDidMount() {
    this.fetchSource();

  }
fetchSource = () => {
getInqSourceList('view=select', this, null)
}

  <RNPickerSelect
                  items={this.state.inqSourceList}
                  name="source"
                  value={this.state.source ? this.state.source.id : null}
                  onValueChange={value => {
                    this.setState({
                      source:value,
                    });
                  }}
                  style={{marginBottom: 10}}
                  
                />

我的api代码是

  export async function getModuleList  (moduleName, params,error) {
  let token = await AsyncStorage.getItem('token');
    axios
      .get(BASE_URL + moduleName + "?" + params, {

        headers: {
           Accept: 'application/json',
          'Content-Type': 'application/json',
           Authorization: "Bearer " + token },
      })
      .then((res) => {
      
          var bytes = CryptoJS.AES.decrypt(res.data.toString(), ENCDEC);
          res.data = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
          // success(res.data);
      //  console.log(res.data)
      })
      .catch(error);
  }

在这个我使用console.log(res.data)时从后端获取值我得到选项列表..但是下拉项目是空的..谢谢

这是设置 inqSourceList 的函数

  export function getInqSourceList(params, _this, next) {
    getModuleList('settings/inquiry-source', params, data => {
      _this.setState({inqSourceList: data.rows});
      if (next) next(data);
    });
  }

标签: react-native

解决方案


推荐阅读