You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native报TypeError: null is not an object (evaluating 'order.originLatitude')如何修复

报错触发原因
  • 代码中order状态的初始值为null,组件首次渲染时,MapViewDirections组件的destination属性直接读取order.originLatitude、order.originLongitude,对null值访问属性直接触发TypeError。
  • 后续如果订单完成将order重置为null时,也会重复触发该报错。
修复方案

仅当order存在时才渲染MapViewDirections路线组件即可,修改MapView部分代码如下:

<MapView
  style={{width: '100%', height: Dimensions.get('window').height - 90}}
  showsUserLocation={true}
  initialRegion={{
      latitude: 15.166958649105302,
      longitude: 120.58020821518215,
      latitudeDelta: 0.015,
      longitudeDelta: 0.015,
  }}
  >
  {/* 新增order存在判断,避免空值访问 */}
  {order && <MapViewDirections
      origin={origin}
      destination={{
          latitude: order.originLatitude,
          longitude: order.originLongitude
      }}
      apikey={GOOGLE_MAPS_APIKEY}
      />}
</MapView>

如果需要无订单时展示默认路线,也可以给order设置包含经纬度的初始默认值代替null,或者使用可选链order?.originLatitude配合默认经纬度值传入组件,可根据业务需求选择实现方式。

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 17:15:08