零件

import React from 'react';
import { getRegions } from '../helpers'

class RegionSelect extends React.Component {
  constructor(props) {
      super(props);
      this.state = { regions: [],
                     selectedRegion: ''
                   };

      this.handleChange = this.handleChange.bind(this);
  }

  componentDidMount() {
    var self = this
    getRegions().then(val => {
      self.setState({regions:val});
    })
  }

  handleChange(event) {
    this.setState({selectedRegion: event.target.value});
  }

  renderRegionPickList() {
    console.log('regions');
    console.log(this.state.regions);
    this.state.regions.map((o) => {
      return (
        <option key={o.label} value={o.value}>{o.label}</option>
      )
    })
  }

  render() {
    return (
      <div className="slds-form-element">
        <svg aria-hidden="true" className="slds-icon slds-icon-text-default slds-icon--small slds-p-around--xx-small">
          <use xlinkHref={locationIcon}></use>
        </svg>
        <label className="slds-form-element__label">Region</label>
        <div className="slds-form-element__control">
          <div className="slds-select_container">
            <select className="slds-select" value={this.state.selectedRegion} onChange={this.handleChange}>
              <option value=''>--select a region--</option>
              {this.renderRegionPickList()}
            </select>
          </div>
        </div>
      </div>
    );
  }
}

export default RegionSelect;


帮手

import axios from 'axios'

export function getRegions() {

  return new Promise(function(resolve,reject){

  // make requst for select values
  var request = axios.get(`${REST_API_URL}sobjects/User/describe/`,
     API_TOKEN
   ).then(function (response){
      const region = response.data.fields.find(field => field.name === 'Region__c');
      return resolve(region.picklistValues);
   })
  })
}


网页

javascript - 选择选项未呈现-LMLPHP

安慰

javascript - 选择选项未呈现-LMLPHP

基于docs,我看不出这是怎么回事。我是否缺少生命周期方法?页面加载时,regions数组为空,然后componentDidMount()重新渲染并且不再为空。

最佳答案

您只需要在map内返回renderRegionPickList的结果,因为当前它返回undefined

renderRegionPickList() {
  return this.state.regions.map((o) => (
    <option key={o.label} value={o.value}>
      {o.label}
    </option>
  ))
}

关于javascript - 选择选项未呈现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42309452/

10-11 20:29