【问题标题】:React state array gives null error in rendorReact 状态数组在渲染中给出空错误
【发布时间】:2020-04-20 03:48:36
【问题描述】:

我正在调用发出 JSON 的 API 我正在 componentDidMount 和 setState 中调用这个 promise api 函数,然后在 render 方法中调用它,但它总是发出 null 错误。

请帮帮我

位置界面

export interface ILocation {
  info: Info;
  options: Options;
  results: Result[];
}

interface Result {
  providedLocation: ProvidedLocation;
  locations: Location[];
}

interface Location {
  street: string;
  adminArea6: string;
  adminArea6Type: string;
  adminArea5: string;
  adminArea5Type: string;
  adminArea4: string;
  adminArea4Type: string;
  adminArea3: string;
  adminArea3Type: string;
  adminArea1: string;
  adminArea1Type: string;
  postalCode: string;
  geocodeQualityCode: string;
  geocodeQuality: string;
  dragPoint: boolean;
  sideOfStreet: string;
  linkId: string;
  unknownInput: string;
  type: string;
  latLng: LatLng;
  displayLatLng: LatLng;
  mapUrl: string;
}

interface ProvidedLocation {
  latLng: LatLng;
}

interface LatLng {
  lat: number;
  lng: number;
}

interface Options {
  maxResults: number;
  thumbMaps: boolean;
  ignoreLatLngInput: boolean;
}

interface Info {
  statuscode: number;
  copyright: Copyright;
  messages: any[];
}

interface Copyright {
  text: string;
  imageUrl: string;
  imageAltText: string;
}

IWeatherStates

export interface IWeatherStates {
  Locations: ILocation
}

获取位置

private getLocation(latitudes: number, longitude: number): Promise<ILocation> {

    let promise: Promise<ILocation> = new Promise<ILocation>((resolve, reject) => {
      let query = `${this.LIST_API_ENDPOINT}/location/${latitudes},${longitude}`
      this.props.httpClient.get(query, HttpClient.configurations.v1, this.httpClientOptions).then((rawResponse: HttpClientResponse) => {
        return rawResponse.json()
      }).then((jsonResponse) => {
        resolve(jsonResponse)
      })
    })
    return promise
  }
}

componentDidMount

  public componentDidMount() {
    this.getLocation(29.378586, 47.990341).then((location: ILocation) => {
      this.setState({
        Locations: location
      })
    })
  }

建筑

constructor(props: IWeatherProps) {
    super(props)

    this.state = {
      Locations: null
    }
  }

**给出错误的渲染**

public render(): React.ReactElement<IWeatherProps> {
    return (
      <div className={styles.weather}>
       {this.state.Locations.results[0].locations[0].adminArea5}
      </div>
    )
}

{this.state.Locations.results[0].locations[0].adminArea5} 结果给出空错误

【问题讨论】:

  • 您希望如何从 null 获取索引 0?
  • 添加检查位置是否为空,呈现“未加载位置”等
  • 在 componentDidMount 如果我把 console.log 它给出输出
  • 把console放到render函数里你会看到null
  • 是的,为什么会这样!!

标签: javascript json reactjs typescript


【解决方案1】:

添加检查以避免null

public render(): React.ReactElement<IWeatherProps> {
    const { Locations } = this.state;
    return (
      <div className={styles.weather}>
       {Locations ? Locations.results[0].locations[0].adminArea5 : 'Not loaded'}
      </div>
    )
}

【讨论】:

  • 亲爱的,我之前解释过,如果您的代码尝试在 something[0] 中执行 null 时,它将中断。所以你需要检查你的变量是否为空。如果为 null 则执行其他操作
  • 我们可以做一个函数来检查null并做输出吗?
  • 我试过了,但没有用。我首先传递两个参数,然后传递另一个字符串。所以首先是空而不是空,否则另一个字符串是空的,但结果是空的,它没有运行:(
  • 我对 TypeScript 了解不多
猜你喜欢
  • 2020-03-09
  • 2018-10-03
  • 1970-01-01
  • 2020-01-25
  • 2018-12-21
  • 2021-06-02
  • 1970-01-01
  • 2022-10-07
  • 2019-01-25
相关资源
最近更新 更多