【发布时间】:2018-08-22 13:16:42
【问题描述】:
我正在使用react-native-maps 编写一个 React Native 应用程序。在我的地图上,我通过在自调用函数中使用 map 方法动态填充了许多 circles。请参阅位于组件的渲染函数中的以下代码:
{
(() => {
return this.state.mapviewPolygon.map(circle => (
<MapView.Circle
center={circle.coordinates}
key={circle.key}
/>
))
})()
}
这在初始渲染时效果很好。我稍后会根据用户在地图上的点击位置更新this.state.mapviewPolygon,目的是让圆圈根据新的坐标集重新渲染。
但是,当这个自调用函数触发时,我收到错误TypeError: undefined is not a function (near '..._this2.state.mapviewPolygon.map...'),这并没有告诉我太多。同样,堆栈跟踪的帮助也可以忽略不计。
发生了什么?如何让圆圈正确重新渲染?
编辑 1:在下面查看我的整个渲染功能:
render() {
return (
<View style={styles.container}>
<MapView
style={styles.map}
showUserLocation
followUserLocation
loadingEnabled
region={this.getMapRegion()}
scrollEnabled = {this.state.mapviewScroll}
onPress= { (e) => {
let stateCopy = Object.assign({}, this.state.mapviewPolygon);
// circleKey is a variable containing the index of
// the item in the state array to change
stateCopy[circleKey].coordinates = e.nativeEvent.coordinate;
this.setState({mapviewPolygon: stateCopy});
}
}
>
{
this.state.mapviewPolygon.map(circle =>
<MapView.Circle
center={circle.coordinates}
key={circle.key}
radius={50}
fillColor={'#4286f4'}
/>
)
}
<MapView.Polygon
coordinates={this.getPolygonCoords()}
fillColor="rgba(0, 200, 0, 0.5)"
strokeColor="rgba(0,0,0,0.5)"
strokeWidth={2}
/>
</MapView>
</View>
);
}
【问题讨论】:
-
试试在你的渲染函数中使用
{ this.state.mapviewPolygon.map(circle => <MapView .../> ) }。 -
同样的事情发生了。
-
你能发布你的整个
render函数吗? -
完成。 (请注意,我在之前的评论中继续实施了您的建议,因为它更简洁,并导致我迄今为止的表现相同。)
-
尝试记录并查看渲染函数中
this.state.mapviewPolygon的值。 map 函数是数组的一个属性。如果你的this.state.mapviewPolygon是 undefined 或 null 那么这个错误就会出现。只要确保该值是一个数组
标签: javascript react-native react-native-maps