I am writing a React Native application using react-native-maps. On my map, I am dynamically populating a number of circles by using the map method inside a self-invoking function. See the following code located in the component's render function:
{
(() => {
return this.state.mapviewPolygon.map(circle => (
<MapView.Circle
center={circle.coordinates}
key={circle.key}
/>
))
})()
}
This works fine on initial rendering. I later update this.state.mapviewPolygon based on where the user clicks on the map, with the intention that the circles will re-render based on the new set of coordinates.
However, when this self-invoking function fires, I receive the error TypeError: undefined is not a function (near '..._this2.state.mapviewPolygon.map...'), which doesn't tell me much. Likewise, the stack trace is of negligible help.
What is happening? How do I get the circles to properly re-render?
EDIT 1: See my entire render function below:
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 .../> ) }in your render function.renderfunction?this.state.mapviewPolygonin the render function. map function is a property of an Array. If yourthis.state.mapviewPolygonis undefined or null then this Error will come. Just make sure that value is an Array